OLDataProvider.m 514 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. #import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. #import "UIProgressView+AFNetworking.h"
  15. #import "AFHTTPSessionManager.h"
  16. #import "AppDelegate.h"
  17. #import "pdfCreator.h"
  18. #import "Singleton.h"
  19. #import "QRCodeGenerator.h"
  20. #import "config.h"
  21. @interface OLDataProvider ()
  22. @end
  23. @implementation OLDataProvider
  24. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  25. {
  26. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  27. NSString* ret=@"No Price.";
  28. sqlite3_stmt * statement;
  29. // int count=0;
  30. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  31. {
  32. if (sqlite3_step(statement) == SQLITE_ROW)
  33. {
  34. char *price = (char*)sqlite3_column_text(statement, 0);
  35. if(price==nil)
  36. price="";
  37. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  38. double dp= [nsprice doubleValue];
  39. ret=[NSString stringWithFormat:@"%.2f",dp];
  40. }
  41. sqlite3_finalize(statement);
  42. }
  43. return ret;
  44. // // [iSalesDB close_db:db];
  45. //
  46. // return nil;
  47. //// if(dprice==DBL_MAX)
  48. //// ret= nil;
  49. //// else
  50. //// ret= [NSNumber numberWithDouble:dprice];
  51. //// return ret;
  52. }
  53. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  54. {
  55. NSString* ret= nil;
  56. NSString *sqlQuery = nil;
  57. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  58. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ and i.type<=1 order by i.default_img desc, i.type desc limit 1;",item_id];
  59. sqlite3_stmt * statement;
  60. // int count=0;
  61. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  62. {
  63. if (sqlite3_step(statement) == SQLITE_ROW)
  64. {
  65. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  66. if(imgurl==nil)
  67. imgurl="";
  68. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  69. ret=nsimgurl;
  70. }
  71. sqlite3_finalize(statement);
  72. }
  73. else
  74. {
  75. ret=@"";
  76. }
  77. // [iSalesDB close_db:db];
  78. // DebugLog(@"data string: %@",ret );
  79. return ret;
  80. }
  81. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  82. {
  83. NSMutableDictionary* values=params[@"replaceValue"];
  84. //生成portfolio pdf需要的数据
  85. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  86. data[@"npd_url"]=@"www.newpacificdirect.com";
  87. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  88. [formatter setDateFormat:@"MMMM yyyy"];
  89. NSString* date = [formatter stringFromDate:[NSDate date]];
  90. data[@"create_month"]=date;
  91. data[@"company_name"]=@"New Pacific Direct,Inc.";
  92. data[@"catalog_name"]=params[@"catalog_name"];
  93. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  94. data[@"TOTAL_PAGE"]=0;
  95. data[@"CURRENT_PAGE"]=0;
  96. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  97. bool show_stockout=[params[@"show_stockout"] boolValue];
  98. NSString *sql = nil;
  99. if(values==nil)
  100. {
  101. sql=[NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id order by a.name",params[@"product_ids"],params[@"product_ids"]];
  102. }
  103. else
  104. {
  105. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in from product where product_id in(%@) order by name",params[@"product_ids"]];
  106. }
  107. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  108. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  109. sqlite3 *db = [iSalesDB get_db];
  110. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  111. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  112. int product_id = sqlite3_column_int(stmt, 0);
  113. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  114. NSString *name = [self textAtColumn:1 statement:stmt];
  115. NSString *description = [self textAtColumn:2 statement:stmt];
  116. double price = sqlite3_column_double(stmt, 3);
  117. NSString *s_price = [self textAtColumn:3 statement:stmt];
  118. double discount = sqlite3_column_double(stmt,4);
  119. int qty = sqlite3_column_int(stmt, 5);
  120. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  121. int is_percentage = sqlite3_column_int(stmt, 6);
  122. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  123. int item_id = sqlite3_column_int(stmt, 7);
  124. // int fashion_id = sqlite3_column_int(stmt, 8);
  125. NSString *line_note = [self textAtColumn:9 statement:stmt];
  126. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  127. double percent = sqlite3_column_double(stmt, 10);
  128. // int portfolio_id = sqlite3_column_int(stmt, 11);
  129. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  130. int availability = sqlite3_column_int(stmt, 12);
  131. NSString *color = [self textAtColumn:13 statement:stmt];
  132. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  133. NSString *demension = [self textAtColumn:15 statement:stmt];
  134. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  135. NSString *material = [self textAtColumn:17 statement:stmt];
  136. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  137. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  138. double volume = sqlite3_column_double(stmt, 19);
  139. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  140. double weight = sqlite3_column_double(stmt, 20);
  141. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  142. int model_set = sqlite3_column_int(stmt, 21);
  143. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  144. double load_ability = sqlite3_column_double(stmt, 22);
  145. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  146. NSString *assembling = [self textAtColumn:24 statement:stmt];
  147. NSString *made_in = [self textAtColumn:25 statement:stmt];
  148. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  149. if(is_percentage==0&&qty==0&&!show_stockout)
  150. {
  151. return;
  152. }
  153. if((is_percentage!=0||s_is_percentage.length==0)&&availability==0&&!show_stockout)
  154. {
  155. return;
  156. }
  157. if(s_percent.length==0||is_percentage==0)
  158. {
  159. percent=100.0;
  160. if([s_qty isEqualToString:@"null"])
  161. qty=availability;
  162. }
  163. else
  164. {
  165. qty=availability;
  166. }
  167. qty=qty*percent/100+0.5;
  168. for(int i=0;i<[values[@"count"] intValue];i++)
  169. {
  170. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  171. if([valueitem[@"product_id"] intValue]==product_id)
  172. {
  173. line_note=valueitem[@"line_note"];
  174. qty=[valueitem[@"available_qty"] intValue];
  175. if([[valueitem allKeys] containsObject:@"available_qty"])
  176. s_qty=[NSString stringWithFormat:@"%d",qty];
  177. else
  178. s_qty=@"null";
  179. if(valueitem[@"regular_price"]!=nil)
  180. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  181. s_price=valueitem[@"special_price"];
  182. price=[valueitem[@"special_price"] floatValue];
  183. discount=0;
  184. }
  185. }
  186. NSString* set_price=@"";
  187. if([params[@"entered_price"] boolValue])
  188. {
  189. if (s_price==nil ) {
  190. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  191. }
  192. else
  193. {
  194. if(price*(1-discount/100.0)!=gprice.floatValue)
  195. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  196. }
  197. }
  198. NSString* get_price=@"";
  199. {
  200. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  201. // DebugLog(@"price time interval");
  202. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  203. if(gprice==nil)
  204. get_price=@"Price:No Price.";
  205. else
  206. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  207. }
  208. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  209. // if ([qty_null isEqualToString:@"null"]) {
  210. // qty=availability;
  211. // }
  212. //
  213. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  214. // if ([qty_percent_null isEqualToString:@"null"]) {
  215. // percentage=1;
  216. // }
  217. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  218. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  219. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  220. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  221. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  222. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  223. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  224. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  225. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  226. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  227. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  228. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  229. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  230. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  231. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  232. if([params[@"availability"] boolValue]==false)
  233. str_availability=@"";
  234. else
  235. {
  236. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  237. str_availability=@"<b>Availability:</b> In Production";
  238. }
  239. if([params[@"color"] boolValue]==false || color.length==0 )
  240. str_color=@"";
  241. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  242. str_model_set=@"";
  243. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  244. str_legcolor=@"";
  245. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  246. str_demension=@"";
  247. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  248. str_seat_height=@"";
  249. if([params[@"material"] boolValue]==false || material.length==0 )
  250. str_meterial=@"";
  251. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  252. str_box_dim=@"";
  253. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  254. str_volume=@"";
  255. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  256. str_weight=@"";
  257. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  258. str_load_ability=@"";
  259. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  260. str_fabric_content=@"";
  261. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  262. str_assembling=@"";
  263. if([params[@"made"] boolValue]==false || made_in.length==0 )
  264. str_made_in=@"";
  265. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  266. str_line_note=@"";
  267. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  268. [arr_detail addObject:str_availability];
  269. [arr_detail addObject:str_color];
  270. [arr_detail addObject:str_model_set];
  271. [arr_detail addObject:str_legcolor];
  272. [arr_detail addObject:str_demension];
  273. [arr_detail addObject:str_seat_height];
  274. [arr_detail addObject:str_meterial];
  275. [arr_detail addObject:str_box_dim];
  276. [arr_detail addObject:str_volume];
  277. [arr_detail addObject:str_weight];
  278. [arr_detail addObject:str_load_ability];
  279. [arr_detail addObject:str_fabric_content];
  280. [arr_detail addObject:str_assembling];
  281. [arr_detail addObject:str_made_in];
  282. [arr_detail addObject:str_line_note];
  283. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  284. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  285. //model image;
  286. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  287. // qr image
  288. NSString* qrpath=nil;
  289. if([params[@"show_barcode"] boolValue])
  290. {
  291. NSString* temp = NSTemporaryDirectory();
  292. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  293. qrpath=[temp stringByAppendingPathComponent:filename];
  294. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  295. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  296. }
  297. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  298. item[@"img"]=img_path;
  299. item[@"img_qr"]=qrpath;
  300. item[@"special_price"]=set_price;
  301. item[@"price"]=get_price;
  302. item[@"name"]=name;
  303. item[@"description"]=description;
  304. item[@"detail"]=detail;
  305. item[@"img"]=img_path;
  306. // @{
  307. // //@"linenotes": line_note,
  308. //// @"product_id": product_id_string,
  309. //// @"available_qty": @(qty),
  310. //// @"available_percent" : @(percent),
  311. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  312. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  313. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  314. // @"": ,
  315. // @"": ,
  316. // @"": ,
  317. // @"": ,
  318. // @"": ,
  319. // @"": ,
  320. // @"":
  321. // }.mutableCopy;
  322. // if (percentage) {
  323. // [item removeObjectForKey:@"available_qty"];
  324. // } else {
  325. // [item removeObjectForKey:@"available_percent"];
  326. // }
  327. //
  328. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  329. // if ([qty_null isEqualToString:@"null"]) {
  330. // [item removeObjectForKey:@"available_qty"];
  331. // }
  332. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  333. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  334. }];
  335. [iSalesDB close_db:db];
  336. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  337. // [dic setValue:@"" forKey:@"email_content"];
  338. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  339. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  340. //
  341. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  342. // cell[@"count"]=[NSNumber numberWithInt:10];
  343. grid[@"cell0"]=dic;
  344. data[@"grid0"]=grid;
  345. return data;
  346. }
  347. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  348. {
  349. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  350. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  351. NSString *cache_folder=[paths objectAtIndex:0];
  352. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  353. BOOL bdir=NO;
  354. NSFileManager* fileManager = [NSFileManager defaultManager];
  355. if(PDF_DEBUG)
  356. {
  357. NSData *data = [NSData dataWithContentsOfFile:default_path];
  358. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  359. return ret;
  360. }
  361. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  362. {
  363. NSError * error=nil;
  364. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  365. {
  366. return nil;
  367. }
  368. }
  369. NSData *data = [NSData dataWithContentsOfFile:template_path];
  370. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  371. return ret;
  372. }
  373. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  374. {
  375. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  376. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info,first_name from offline_login where lower(username)='%@' ",user.lowercaseString ];
  377. DebugLog(@"offline_login sql:%@",sqlQuery);
  378. sqlite3_stmt * statement;
  379. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  380. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  381. {
  382. if (sqlite3_step(statement) == SQLITE_ROW)
  383. {
  384. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  385. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  386. int can_show_price = sqlite3_column_int(statement, 0);
  387. int can_see_price = sqlite3_column_int(statement, 1);
  388. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  389. if(contact_id==nil)
  390. contact_id="";
  391. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  392. int user_type = sqlite3_column_int(statement, 3);
  393. int can_cancel_order = sqlite3_column_int(statement, 4);
  394. int can_set_cart_price = sqlite3_column_int(statement, 5);
  395. int can_create_portfolio = sqlite3_column_int(statement, 6);
  396. int can_delete_order = sqlite3_column_int(statement, 7);
  397. int can_submit_order = sqlite3_column_int(statement, 8);
  398. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  399. int can_create_order = sqlite3_column_int(statement, 10);
  400. char *mode = (char*)sqlite3_column_text(statement, 11);
  401. if(mode==nil)
  402. mode="";
  403. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  404. char *username = (char*)sqlite3_column_text(statement, 12);
  405. if(username==nil)
  406. username="";
  407. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  408. int can_update_contact_info = sqlite3_column_int(statement, 13);
  409. char *first_name = (char*)sqlite3_column_text(statement, 14);
  410. if(first_name==nil)
  411. first_name="";
  412. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  413. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  414. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  415. // [header setValue:nscontact_id forKey:@"contact_id"];
  416. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  417. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  418. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  419. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  420. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  421. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  422. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  423. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  424. //
  425. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  426. //
  427. // [header setValue:nsusername forKey:@"username"];
  428. //
  429. // NSError* error=nil;
  430. //
  431. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  432. // [header setValue:statusFilter forKey:@"statusFilter"];
  433. //
  434. //
  435. //
  436. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  437. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  438. //
  439. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  440. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  441. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  442. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  443. //
  444. // [ret setObject:header forKey:@"header"];
  445. [ret setValue:nsfirst_name forKey:@"first_name"];
  446. }
  447. sqlite3_finalize(statement);
  448. }
  449. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  450. return ret;
  451. }
  452. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  453. {
  454. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  455. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  456. data[@"npd_url"]=@"www.newpacificdirect.com";
  457. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  458. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  459. NSString* date = [formatter stringFromDate:[NSDate date]];
  460. data[@"print_date"]=date;
  461. data[@"company_name"]=@"New Pacific Direct,Inc.";
  462. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  463. data[@"TOTAL_PAGE"]=0;
  464. data[@"CURRENT_PAGE"]=0;
  465. // [formatter setDateFormat:@"MM/dd/yyyy"];
  466. // date = [formatter stringFromDate:[NSDate date]];
  467. // data[@"creat_date"]=date;
  468. NSString* temp = NSTemporaryDirectory();
  469. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  470. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  471. UIImage* qrimg=[RAUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  472. bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  473. data[@"img_barcode"]=qrpath;
  474. data[@"so_id"]=order_code;
  475. data[@"company"]=@"NEW PACIFIC DIRECT, INC.";
  476. // NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where so_id=%@) o left join offline_contact c on o.customer_cid=c.contact_id",order_code];
  477. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus,sender_name,sender_ext,sender_phone,sender_fax,sender_email,warehouse_name,billing_name,billing_ext,billing_phone,billing_fax,billing_email,receive_name,receive_ext,receive_phone,receive_fax,receive_email,poNumber,sales_rep,carrier,vendor_no,terms,sales_terms,port_destination,etd,sign_picpath,status,create_time,shipping,lift_gate_value,handling_fee_value,billing_contact,receive_contact,general_notes from offline_order where so_id='%@'",order_code];
  478. sqlite3 *db = [iSalesDB get_db];
  479. data[@"print_by"]= [self query_login:appDelegate.user db:db][@"first_name"];
  480. sqlite3_stmt * statement;
  481. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  482. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  483. row0[@"count"]=[NSNumber numberWithInt:1];
  484. datasource[@"row0"]=row0;
  485. NSString* print_status=@"";
  486. NSString* order_type=@"Offline Quote";
  487. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  488. {
  489. if (sqlite3_step(statement) == SQLITE_ROW)
  490. {
  491. int erpOrderStatus = sqlite3_column_int(statement, 0);
  492. int status = sqlite3_column_int(statement, 26);
  493. if(status==2)
  494. {
  495. switch (erpOrderStatus) {
  496. case 10:
  497. // order_type=@"Quoted Order";
  498. print_status=@"Quote";
  499. break;
  500. case 11:
  501. print_status=@"Booked";
  502. break;
  503. case 12:
  504. print_status=@"Picked";
  505. break;
  506. case 13:
  507. print_status=@"Shipped";
  508. break;
  509. case 14:
  510. print_status=@"Closed";
  511. break;
  512. case 15:
  513. print_status=@"Canceled";
  514. break;
  515. case 16:
  516. print_status=@"All";
  517. break;
  518. case 18:
  519. print_status=@"Processing";
  520. break;
  521. default:
  522. break;
  523. }
  524. }
  525. else{
  526. switch (status) {
  527. case 0:
  528. {
  529. print_status=@"Quote";
  530. break;
  531. }
  532. case 1:
  533. {
  534. print_status=@"Saved";
  535. break;
  536. }
  537. case 3:
  538. {
  539. //canceled
  540. print_status=@"Canceled";
  541. break;
  542. }
  543. default:
  544. break;
  545. }
  546. }
  547. NSString* sender_name = [self textAtColumn:1 statement:statement];
  548. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  549. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  550. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  551. NSString* sender_email = [self textAtColumn:5 statement:statement];
  552. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  553. NSString* billing_name = [self textAtColumn:7 statement:statement];
  554. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  555. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  556. if(billing_phone.length>0)
  557. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  558. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  559. if(billing_fax.length>0)
  560. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  561. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  562. [billto_arr0 addObject:billing_phone];
  563. [billto_arr0 addObject:billing_fax];
  564. NSString* billing_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  565. NSString* billing_email = [self textAtColumn:11 statement:statement];
  566. if(billing_email.length>0)
  567. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  568. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  569. if(billing_contact.length>0)
  570. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  571. NSString* receive_name = [self textAtColumn:12 statement:statement];
  572. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  573. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  574. if(receive_phone.length>0)
  575. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  576. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  577. if(receive_fax.length>0)
  578. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  579. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  580. [receive_arr0 addObject:receive_phone];
  581. [receive_arr0 addObject:receive_fax];
  582. NSString* receive_email = [self textAtColumn:16 statement:statement];
  583. NSString* receive_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  584. if(receive_email.length>0)
  585. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  586. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  587. if(receive_contact.length>0)
  588. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  589. NSString* poNumber = [self textAtColumn:17 statement:statement];
  590. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  591. NSString* carrier = [self textAtColumn:19 statement:statement];
  592. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  593. NSString* terms = [self textAtColumn:21 statement:statement];
  594. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  595. NSString* port_destination = [self textAtColumn:23 statement:statement];
  596. NSString* etd = [self textAtColumn:24 statement:statement];
  597. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  598. if(sign_picpath.length==0)
  599. sign_picpath=nil;
  600. NSString* create_date = [self textAtColumn:27 statement:statement];
  601. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  602. NSString* general_notes=[self textAtColumn:33 statement:statement];
  603. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  604. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  605. if(general_notes.length>0)
  606. {
  607. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  608. row2[@"item_0"]=@{@"g_notes":general_notes };
  609. row2[@"count"]=[NSNumber numberWithInt:1];
  610. datasource[@"row2"]=row2;
  611. }
  612. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  613. [billto_arr addObject:billing_ext];
  614. [billto_arr addObject:billing_pf];
  615. [billto_arr addObject:billing_contact];
  616. [billto_arr addObject:billing_email];
  617. NSString* billto=[RAUtils arr2string:billto_arr separator:@"\r\n" trim:true];
  618. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  619. [receive_arr addObject:receive_ext];
  620. [receive_arr addObject:receive_pf];
  621. [receive_arr addObject:receive_contact];
  622. [receive_arr addObject:receive_email];
  623. NSString* receive=[RAUtils arr2string:receive_arr separator:@"\r\n" trim:true];
  624. float shipping = sqlite3_column_double(statement, 28);
  625. float lift_gate_value = sqlite3_column_double(statement, 29);
  626. float handling_fee_value = sqlite3_column_double(statement, 30);
  627. data[@"company"]=sender_name;
  628. data[@"shipfrom_addr"]=sender_ext;
  629. data[@"shipfrom_phone"]=sender_phone;
  630. data[@"shipfrom_fax"]=sender_fax;
  631. data[@"shipfrom_email"]=sender_email;
  632. data[@"warehouse_name"]=warehouse_name;
  633. data[@"bill_to_name"]=billing_name;
  634. data[@"bill_to_ext"]=billto;
  635. data[@"ship_to_name"]=receive_name;
  636. data[@"ship_to_ext"]=receive;
  637. data[@"po_no"]=poNumber;
  638. data[@"rep"]=sales_rep;
  639. data[@"e_schdule"]=etd;
  640. data[@"sales_terms"]=sales_terms;
  641. data[@"port_destination"]=port_destination;
  642. data[@"terms"]=terms;
  643. data[@"carrier"]=carrier;
  644. data[@"vendor_no"]=vendor_no;
  645. data[@"customer_sign"]=sign_picpath;
  646. data[@"print_status"]=print_status;
  647. data[@"create_date"]=create_date;
  648. NSString *sql_cart=[ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id ",order_code];;
  649. sqlite3_stmt * statement_cart;
  650. // NSDate *date1 = [NSDate date];
  651. int count=0;
  652. int cart_count=0;
  653. float credits=0;
  654. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  655. if ( dbresult== SQLITE_OK)
  656. {
  657. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  658. int row1count=0;
  659. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  660. {
  661. NSDate *row_date = [NSDate date];
  662. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  663. // int product_id = sqlite3_column_int(statement_cart, 0);
  664. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  665. int item_id = sqlite3_column_int(statement_cart, 7);
  666. double discount = sqlite3_column_double(statement_cart, 2);
  667. NSString* Price=nil;
  668. if(str_price==nil)
  669. {
  670. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  671. // NSDate *price_date = [NSDate date];
  672. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  673. // DebugLog(@"price time interval");
  674. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  675. if(price==nil)
  676. Price=@"No Price.";
  677. else
  678. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  679. }
  680. else
  681. {
  682. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  683. }
  684. float dunitprice=0;
  685. int item_count = sqlite3_column_int(statement_cart, 3);
  686. NSString* Amount=@"";
  687. if(![Price isEqualToString:@"No Price."])
  688. {
  689. dunitprice=[Price floatValue];
  690. dunitprice=dunitprice* (1-discount/100.0);
  691. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  692. credits+=dunitprice*item_count;
  693. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  694. }
  695. else
  696. {
  697. Price=@"";
  698. }
  699. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  700. // NSString *nsline_note=nil;
  701. // if(line_note!=nil)
  702. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  703. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  704. NSString *nsname=nil;
  705. if(name!=nil)
  706. nsname= [[NSString alloc]initWithUTF8String:name];
  707. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  708. NSString *nsdescription=nil;
  709. if(description!=nil)
  710. nsdescription= [[NSString alloc]initWithUTF8String:description];
  711. // int stockUom = sqlite3_column_int(statement_cart, 8);
  712. // int _id = sqlite3_column_int(statement_cart, 9);
  713. // NSDate *subtotal_date = [NSDate date];
  714. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  715. // DebugLog(@"subtotal_date time interval");
  716. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  717. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  718. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  719. // int carton=[bsubtotaljson[@"carton"] intValue];
  720. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  721. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  722. // NSDate *img_date = [NSDate date];
  723. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  724. // DebugLog(@"img_date time interval");
  725. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  726. itemjson[@"model"]=nsname;
  727. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  728. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  729. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  730. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  731. // itemjson[@"check"]=@"true";
  732. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  733. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  734. itemjson[@"unit_price"]=Price;
  735. itemjson[@"amount"]=Amount;
  736. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  737. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  738. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  739. // itemjson[@"note"]=nsline_note;
  740. // NSDate *date2 = [NSDate date];
  741. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  742. // DebugLog(@"model_bundle time interval");
  743. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  744. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  745. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  746. row1count++;
  747. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false];
  748. for(int c=0;c<[combine[@"count"] intValue];c++)
  749. {
  750. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  751. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  752. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  753. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue]];
  754. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  755. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  756. row1count++;
  757. }
  758. count++;
  759. // DebugLog(@"row time interval");
  760. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  761. }
  762. row1[@"count"]=[NSNumber numberWithInt:row1count];
  763. datasource[@"row1"]=row1;
  764. data[@"table_model"]=datasource;
  765. sqlite3_finalize(statement_cart);
  766. }
  767. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  768. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  769. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  770. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  771. if(credits>0)
  772. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  773. else
  774. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  775. if(shipping>0)
  776. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  777. else
  778. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  779. if(lift_gate_value>0)
  780. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  781. else
  782. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  783. if(handling_fee_value>0)
  784. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  785. else
  786. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  787. float total = credits+shipping+lift_gate_value+handling_fee_value;
  788. if(total>0)
  789. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  790. else
  791. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  792. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  793. fee[@"row0"]=fee_row0;
  794. data[@"table_fee"]=fee;
  795. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  796. // ret[@"sign_url"] = sign_url;
  797. //
  798. // customer_contact = [self textAtColumn:52 statement:statement];
  799. // customer_email = [self textAtColumn:53 statement:statement];
  800. // customer_phone = [self textAtColumn:54 statement:statement];
  801. // customer_fax = [self textAtColumn:55 statement:statement];
  802. //
  803. //
  804. // int offline_edit=sqlite3_column_int(statement, 56);
  805. //
  806. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  807. //
  808. // char *soid = (char*)sqlite3_column_text(statement, 1);
  809. // if(soid==nil)
  810. // soid= "";
  811. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  812. // // so#
  813. // ret[@"so#"] = nssoid;
  814. //
  815. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  816. // if(poNumber==nil)
  817. // poNumber= "";
  818. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  819. //
  820. //
  821. //
  822. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  823. // if(create_time==nil)
  824. // create_time= "";
  825. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  826. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  827. //
  828. // int status = sqlite3_column_int(statement, 4);
  829. // int erpStatus = sqlite3_column_int(statement, 49);
  830. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  831. //
  832. // // status
  833. // if (status > 1 && status != 3) {
  834. // status = erpStatus;
  835. // } else if (status == 3) {
  836. // status = 15;
  837. // }
  838. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  839. // ret[@"order_status"] = nsstatus;
  840. //
  841. //
  842. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  843. // if(company_name==nil)
  844. // company_name= "";
  845. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  846. // // company name
  847. // ret[@"company_name"] = nscompany_name;
  848. //
  849. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  850. // if(customer_contact==nil)
  851. // customer_contact= "";
  852. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  853. //
  854. //
  855. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  856. // if(addr_1==nil)
  857. // addr_1="";
  858. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  859. //
  860. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  861. // if(addr_2==nil)
  862. // addr_2="";
  863. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  864. //
  865. //
  866. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  867. // if(addr_3==nil)
  868. // addr_3="";
  869. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  870. //
  871. //
  872. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  873. // if(addr_4==nil)
  874. // addr_4="";
  875. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  876. //
  877. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  878. // [arr_addr addObject:nsaddr_1];
  879. // [arr_addr addObject:nsaddr_2];
  880. // [arr_addr addObject:nsaddr_3];
  881. // [arr_addr addObject:nsaddr_4];
  882. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  883. //
  884. //
  885. // char *logist = (char*)sqlite3_column_text(statement, 11);
  886. // if(logist==nil)
  887. // logist= "";
  888. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  889. // if (status == -11 || status == 10 || status == 11) {
  890. // nslogist = [self textAtColumn:59 statement:statement];
  891. // };
  892. //
  893. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  894. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  895. // shipping = @"Shipping To Be Quoted";
  896. // } else {
  897. // shippingFee = sqlite3_column_double(statement, 12);
  898. // }
  899. //
  900. // // Shipping
  901. // ret[@"Shipping"] = shipping;
  902. //
  903. // int have_lift_gate = sqlite3_column_int(statement, 17);
  904. // lift_gate = sqlite3_column_double(statement, 13);
  905. // // Liftgate Fee(No loading dock)
  906. // if (!have_lift_gate) {
  907. // lift_gate = 0;
  908. // }
  909. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  910. // if (sqlite3_column_int(statement, 57)) {
  911. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  912. // }
  913. //
  914. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  915. // if(general_notes==nil)
  916. // general_notes= "";
  917. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  918. //
  919. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  920. // if(internal_notes==nil)
  921. // internal_notes= "";
  922. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  923. //
  924. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  925. // if(payment_type==nil)
  926. // payment_type= "";
  927. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  928. //
  929. //
  930. //
  931. // // order info
  932. // orderinfo = [self textFileName:@"order_info.html"];
  933. //
  934. //
  935. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  936. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  937. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  938. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  939. //
  940. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  941. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  942. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  943. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  944. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  945. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  946. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  947. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  948. //
  949. // NSString *payment = nil;
  950. // if([nspayment_type isEqualToString:@"Credit Card"])
  951. // {
  952. // payment = [self textFileName:@"creditcardpayment.html"];
  953. //
  954. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  955. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  956. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  957. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  958. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  959. // NSString *card_type = [self textAtColumn:42 statement:statement];
  960. // if (card_type.length > 0) { // 显示星号
  961. // card_type = @"****";
  962. // }
  963. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  964. // if (card_number.length > 0 && card_number.length > 4) {
  965. // for (int i = 0; i < card_number.length - 4; i++) {
  966. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  967. // }
  968. // } else {
  969. // card_number = @"";
  970. // }
  971. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  972. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  973. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  974. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  975. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  976. // card_expiration = @"****";
  977. // }
  978. //
  979. //
  980. // NSString *card_city = [self textAtColumn:46 statement:statement];
  981. // NSString *card_state = [self textAtColumn:47 statement:statement];
  982. //
  983. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  984. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  985. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  986. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  987. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  988. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  989. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  990. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  991. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  992. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  993. //
  994. //
  995. // }
  996. // else
  997. // {
  998. // payment=[self textFileName:@"normalpayment.html"];
  999. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1000. // }
  1001. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1002. //
  1003. // ret[@"result"]= [NSNumber numberWithInt:2];
  1004. //
  1005. // // more info
  1006. // moreInfo = [self textFileName:@"more_info.html"];
  1007. //
  1008. //
  1009. // // ShipToCompany_or_&nbsp
  1010. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1011. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1012. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1013. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1014. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1015. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1016. //
  1017. //
  1018. // // ShipFromCompany_or_&nbsp
  1019. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1020. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1021. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1022. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1023. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1024. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1025. //
  1026. //
  1027. // // FreightBillToCompany_or_&nbsp
  1028. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1029. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1030. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1031. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1032. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1033. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1034. //
  1035. //
  1036. // // MerchandiseBillToCompany_or_&nbsp
  1037. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1038. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1039. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1040. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1041. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1042. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1043. //
  1044. //
  1045. // // ReturnToCompany_or_&nbsp
  1046. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1047. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1048. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1049. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1050. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1051. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1052. // //
  1053. //
  1054. // // DebugLog(@"more info : %@",moreInfo);
  1055. //
  1056. // // handling fee
  1057. // handlingFee = sqlite3_column_double(statement, 33);
  1058. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1059. // if (sqlite3_column_int(statement, 58)) {
  1060. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1061. // }
  1062. // //
  1063. //
  1064. // // customer info
  1065. // customerID = [self textAtColumn:36 statement:statement];
  1066. //
  1067. // // mode
  1068. // ret[@"mode"] = appDelegate.mode;
  1069. //
  1070. // // model_count
  1071. // ret[@"model_count"] = @(0);
  1072. }
  1073. sqlite3_finalize(statement);
  1074. }
  1075. [iSalesDB close_db:db];
  1076. data[@"order_type"]=order_type;
  1077. /*
  1078. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1079. // "customer_email" = "Shui Hu";
  1080. // "customer_fax" = "";
  1081. // "customer_first_name" = F;
  1082. // "customer_last_name" = L;
  1083. // "customer_name" = ",da He Xiang Dong Liu A";
  1084. // "customer_phone" = "Hey Xuan Feng";
  1085. contactInfo[@"customer_phone"] = customer_phone;
  1086. contactInfo[@"customer_fax"] = customer_fax;
  1087. contactInfo[@"customer_email"] = customer_email;
  1088. NSString *first_name = @"";
  1089. NSString *last_name = @"";
  1090. if ([customer_contact isEqualToString:@""]) {
  1091. } else if ([customer_contact containsString:@" "]) {
  1092. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1093. first_name = [customer_contact substringToIndex:first_space_index];
  1094. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1095. }
  1096. contactInfo[@"customer_first_name"] = first_name;
  1097. contactInfo[@"customer_last_name"] = last_name;
  1098. ret[@"customerInfo"] = contactInfo;
  1099. // models
  1100. if (nssoid) {
  1101. __block double TotalCuft = 0;
  1102. __block double TotalWeight = 0;
  1103. __block int TotalCarton = 0;
  1104. __block double allItemPrice = 0;
  1105. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",nssoid];
  1106. sqlite3 *db1 = [iSalesDB get_db];
  1107. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1108. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1109. int product_id = sqlite3_column_int(stmt, 0);
  1110. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1111. int item_id = sqlite3_column_int(stmt, 7);
  1112. NSString* Price=nil;
  1113. if(str_price==nil)
  1114. {
  1115. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1116. if(price==nil)
  1117. Price=@"No Price.";
  1118. else
  1119. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1120. }
  1121. else
  1122. {
  1123. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1124. }
  1125. double discount = sqlite3_column_double(stmt, 2);
  1126. int item_count = sqlite3_column_int(stmt, 3);
  1127. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1128. NSString *nsline_note=nil;
  1129. if(line_note!=nil)
  1130. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1131. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1132. // NSString *nsname = nil;
  1133. // if(name!=nil)
  1134. // nsname= [[NSString alloc]initWithUTF8String:name];
  1135. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1136. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1137. // NSString *nsdescription=nil;
  1138. // if(description!=nil)
  1139. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1140. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1141. // int stockUom = sqlite3_column_int(stmt, 8);
  1142. // int _id = sqlite3_column_int(stmt, 9);
  1143. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1144. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1145. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1146. int carton=[bsubtotaljson[@"carton"] intValue];
  1147. TotalCuft += cuft;
  1148. TotalWeight += weight;
  1149. TotalCarton += carton;
  1150. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1151. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1152. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1153. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1154. itemjson[@"note"]=nsline_note;
  1155. itemjson[@"origin_price"] = Price;
  1156. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1157. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1158. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1159. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1160. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1161. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1162. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1163. if(itemjson[@"combine"] != nil)
  1164. {
  1165. // int citem=0;
  1166. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1167. for(int bc=0;bc<bcount;bc++)
  1168. {
  1169. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1170. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1171. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1172. subTotal += uprice * modulus * item_count;
  1173. }
  1174. }
  1175. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1176. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1177. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1178. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1179. allItemPrice += subTotal;
  1180. }];
  1181. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1182. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1183. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1184. // payments/Credits
  1185. // payments_and_credist = sqlite3_column_double(statement, 34);
  1186. payments_and_credist = allItemPrice;
  1187. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1188. // // total
  1189. // totalPrice = sqlite3_column_double(statement, 35);
  1190. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1191. } else {
  1192. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1193. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1194. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1195. // payments/Credits
  1196. payments_and_credist = 0;
  1197. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1198. }
  1199. // total
  1200. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1201. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1202. ret[@"order_info"]= orderinfo;
  1203. ret[@"more_order_info"] = moreInfo;
  1204. return [RAUtils dict2data:ret];
  1205. */
  1206. DebugLog(@"debug sales order data:%@", [RAUtils dict2string:data]);
  1207. return data;
  1208. }
  1209. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1210. {
  1211. NSString* template_file=nil;
  1212. switch([params[@"thumb"] intValue])
  1213. {
  1214. case 0:
  1215. template_file= @"so.json";
  1216. break;
  1217. case 1:
  1218. template_file= @"so_thumb.json";
  1219. break;
  1220. default:
  1221. template_file= @"so.json";
  1222. }
  1223. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"thumb"] intValue]];
  1224. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1225. if(data[@"table_model"][@"row2"]==nil)
  1226. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1227. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1228. DebugLog(@"%@",file);
  1229. // return nil;
  1230. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1231. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1232. if (file) {
  1233. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1234. [dic setObject:file forKey:@"pdf_path"];
  1235. dic[@"isLocalFile"]=@"true";
  1236. return [RAUtils dict2data:dic];
  1237. }
  1238. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1239. dic[@"isLocalFile"]=@"true";
  1240. return [RAUtils dict2data:dic];
  1241. }
  1242. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1243. {
  1244. NSString* template_file=nil;
  1245. switch([params[@"pdf_style"] intValue])
  1246. {
  1247. case 0:
  1248. template_file= @"portfolio_2x3.json";
  1249. break;
  1250. case 1:
  1251. template_file= @"portfolio_3x2.json";
  1252. break;
  1253. default:
  1254. template_file= @"portfolio_3x2.json";
  1255. }
  1256. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1257. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1258. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1259. DebugLog(@"%@",file);
  1260. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1261. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1262. if (file) {
  1263. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1264. [dic setObject:file forKey:@"pdf_path"];
  1265. dic[@"isLocalFile"]=@"true";
  1266. return [RAUtils dict2data:dic];
  1267. }
  1268. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1269. dic[@"isLocalFile"]=@"true";
  1270. return [RAUtils dict2data:dic];
  1271. }
  1272. +(NSString*) get_offline_soid:(sqlite3*)db
  1273. {
  1274. NSString* soid=nil;
  1275. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1276. [formatter setDateFormat:@"yyMMdd"];
  1277. NSString* date = [formatter stringFromDate:[NSDate date]];
  1278. for(int i=1;i<999;i++)
  1279. {
  1280. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1281. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1282. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1283. if(count==0)
  1284. return soid;
  1285. }
  1286. return nil;
  1287. }
  1288. +(NSArray*) enumOfflineOrder
  1289. {
  1290. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1291. sqlite3 *db = [iSalesDB get_db];
  1292. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1293. sqlite3_stmt * statement;
  1294. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1295. if ( dbresult== SQLITE_OK)
  1296. {
  1297. while (sqlite3_step(statement) == SQLITE_ROW)
  1298. {
  1299. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1300. NSString* nsso_id=nil;
  1301. if(so_id!=nil)
  1302. {
  1303. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1304. }
  1305. [ret addObject:nsso_id];
  1306. }
  1307. sqlite3_finalize(statement);
  1308. }
  1309. [iSalesDB close_db:db];
  1310. return ret;
  1311. }
  1312. +(void) uploadFile:(NSString*) file
  1313. {
  1314. NSData* data = [NSData dataWithContentsOfFile: file];
  1315. UIApplication * app = [UIApplication sharedApplication];
  1316. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1317. AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1318. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1319. if(appDelegate.user!=nil)
  1320. [params setValue:appDelegate.user forKey:@"user"];
  1321. // if(appDelegate.contact_id!=nil)
  1322. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1323. if(appDelegate.password!=nil)
  1324. [params setValue:appDelegate.password forKey:@"password"];
  1325. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1326. [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1327. } error:nil];
  1328. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1329. NSProgress *progress = nil;
  1330. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1331. if (error) {
  1332. NSString* err_msg = [error localizedDescription];
  1333. DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1334. NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1335. DebugLog(@"data string: %@",str);
  1336. [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1337. } else {
  1338. DebugLog(@"response ");
  1339. NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1340. // 再将NSData转为字符串
  1341. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1342. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1343. DebugLog(@"data string: %@",jsonStr);
  1344. NSDictionary* json = responseObject;
  1345. if([[json valueForKey:@"result"] intValue]==2)
  1346. {
  1347. // NSString* img_url_down = json[@"img_url_aname"];
  1348. // NSString* img_url_up = json[@"img_url"];
  1349. }
  1350. else
  1351. {
  1352. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1353. }
  1354. }
  1355. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1356. }];
  1357. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1358. //
  1359. [uploadTask resume];
  1360. }
  1361. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  1362. {
  1363. NSString* serial= [[NSUUID UUID] UUIDString];
  1364. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1365. NSString *cachefolder = [paths objectAtIndex:0];
  1366. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1367. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1368. NSFileManager* fileManager = [NSFileManager defaultManager];
  1369. BOOL bdir=YES;
  1370. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1371. {
  1372. NSError *error = nil;
  1373. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1374. if(!bsuccess)
  1375. DebugLog(@"Create UPLOAD folder failed");
  1376. }
  1377. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1378. ret[@"contact"]=[self prepareContact:serial];
  1379. ret[@"wishlist"]=[self prepareWishlist:serial];
  1380. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  1381. ret[@"portfolio"] = [self preparePortfolio:serial];
  1382. ret[@"view_portfolio"] = [self preparePDF:serial];
  1383. NSString* str= [RAUtils dict2string:ret];
  1384. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1385. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1386. NSError *error=nil;
  1387. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1388. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1389. ZipArchive* zip = [[ZipArchive alloc] init];
  1390. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1391. for(int i=0;i<arr_files.count;i++)
  1392. {
  1393. NSString* file=arr_files[i];
  1394. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1395. }
  1396. if( ![zip CloseZipFile2] )
  1397. {
  1398. zippath = @"";
  1399. }
  1400. ret[@"file"]=zippath;
  1401. return ret;
  1402. }
  1403. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1404. {
  1405. if(filename.length==0)
  1406. return false;
  1407. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1408. bool ret=false;
  1409. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1410. NSString *cachefolder = [paths objectAtIndex:0];
  1411. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1412. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1413. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1414. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1415. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1416. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1417. NSFileManager* fileManager = [NSFileManager defaultManager];
  1418. BOOL bdir=NO;
  1419. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1420. {
  1421. NSError *error = nil;
  1422. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1423. {
  1424. ret=false;
  1425. }
  1426. else
  1427. {
  1428. ret=true;
  1429. }
  1430. }
  1431. return ret;
  1432. }
  1433. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1434. {
  1435. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1436. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1437. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1438. NSString* where=@" 1=1";
  1439. if (ver!=nil) {
  1440. where=@"is_dirty=1";
  1441. }
  1442. sqlite3 *db = [iSalesDB get_db];
  1443. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1444. sqlite3_stmt * statement;
  1445. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1446. int count=0;
  1447. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1448. if ( dbresult== SQLITE_OK)
  1449. {
  1450. while (sqlite3_step(statement) == SQLITE_ROW)
  1451. {
  1452. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1453. int _id = sqlite3_column_int(statement, 0);
  1454. int product_id = sqlite3_column_int(statement, 1);
  1455. int item_id = sqlite3_column_int(statement, 2);
  1456. int qty = sqlite3_column_int(statement, 3);
  1457. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1458. NSString* nscreate_time=nil;
  1459. if(create_time!=nil)
  1460. {
  1461. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1462. }
  1463. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1464. NSString* nsmodify_time=nil;
  1465. if(modify_time!=nil)
  1466. {
  1467. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1468. }
  1469. int is_delete = sqlite3_column_int(statement, 6);
  1470. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1471. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1472. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1473. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1474. itemjson[@"create_time"]=nscreate_time;
  1475. itemjson[@"modify_time"]=nsmodify_time;
  1476. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1477. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1478. count++;
  1479. }
  1480. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1481. sqlite3_finalize(statement);
  1482. }
  1483. ret[@"count"]=[NSNumber numberWithInt:count ];
  1484. [iSalesDB close_db:db];
  1485. return ret;
  1486. }
  1487. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1488. {
  1489. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1490. UIApplication * app = [UIApplication sharedApplication];
  1491. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1492. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",orderCode ];
  1493. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  1494. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1495. DebugLog(@"offline_login sql:%@",sqlQuery);
  1496. sqlite3_stmt * statement;
  1497. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1498. int count=0;
  1499. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1500. if ( dbresult== SQLITE_OK)
  1501. {
  1502. while (sqlite3_step(statement) == SQLITE_ROW)
  1503. {
  1504. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1505. int product_id = sqlite3_column_int(statement, 0);
  1506. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1507. int item_id = sqlite3_column_int(statement, 7);
  1508. NSString* Price=nil;
  1509. if(str_price==nil)
  1510. {
  1511. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1512. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1513. if(price==nil)
  1514. Price=@"No Price.";
  1515. else
  1516. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1517. }
  1518. else
  1519. {
  1520. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1521. }
  1522. double discount = sqlite3_column_double(statement, 2);
  1523. int item_count = sqlite3_column_int(statement, 3);
  1524. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1525. NSString *nsline_note=nil;
  1526. if(line_note!=nil)
  1527. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1528. char *name = (char*)sqlite3_column_text(statement, 5);
  1529. NSString *nsname=nil;
  1530. if(name!=nil)
  1531. nsname= [[NSString alloc]initWithUTF8String:name];
  1532. char *description = (char*)sqlite3_column_text(statement, 6);
  1533. NSString *nsdescription=nil;
  1534. if(description!=nil)
  1535. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1536. // int stockUom = sqlite3_column_int(statement, 8);
  1537. // int _id = sqlite3_column_int(statement, 9);
  1538. //
  1539. //
  1540. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1541. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1542. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1543. // int carton=[bsubtotaljson[@"carton"] intValue];
  1544. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1545. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1546. itemjson[@"model"]=nsname;
  1547. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1548. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1549. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1550. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1551. // itemjson[@"check"]=@"true";
  1552. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1553. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1554. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1555. itemjson[@"unit_price"]=Price;
  1556. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1557. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1558. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1559. itemjson[@"note"]=nsline_note;
  1560. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  1561. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1562. count++;
  1563. }
  1564. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1565. sqlite3_finalize(statement);
  1566. }
  1567. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1568. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1569. return ret;
  1570. }
  1571. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1572. {
  1573. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1574. sqlite3 *db = [iSalesDB get_db];
  1575. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1576. // for(int i=0;i<arr_soid.count;i++)
  1577. sqlite3_stmt * statement;
  1578. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1579. //int count=0;
  1580. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1581. if ( dbresult== SQLITE_OK)
  1582. {
  1583. int count=0;
  1584. while (sqlite3_step(statement) == SQLITE_ROW)
  1585. {
  1586. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1587. int _id = sqlite3_column_int(statement, 0);
  1588. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1589. NSString* nssync_data=nil;
  1590. if(sync_data!=nil)
  1591. {
  1592. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1593. }
  1594. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1595. NSString* nsimg_1=nil;
  1596. if(img_1!=nil)
  1597. {
  1598. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1599. [self copy_upImg:serial file:nsimg_1];
  1600. }
  1601. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1602. NSString* nsso_no=nil;
  1603. if(so_no!=nil)
  1604. {
  1605. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1606. }
  1607. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1608. itemjson[@"order_type"]=@"submit order";
  1609. else
  1610. itemjson[@"order_type"]=@"archive order";
  1611. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1612. itemjson[@"json_data"]= nssync_data;
  1613. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1614. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1615. count++;
  1616. }
  1617. ret[@"count"]=[NSNumber numberWithInt:count ];
  1618. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1619. }
  1620. sqlite3_finalize(statement);
  1621. [iSalesDB close_db:db];
  1622. return ret;
  1623. }
  1624. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1625. {
  1626. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1627. sqlite3 *db = [iSalesDB get_db];
  1628. // UIApplication * app = [UIApplication sharedApplication];
  1629. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1630. NSString *sqlQuery =@"select _id,img_0,img_1,img_2,sync_data from offline_contact where is_active=1 and not sync_data is null";
  1631. sqlite3_stmt * statement;
  1632. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1633. int count=0;
  1634. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1635. if ( dbresult== SQLITE_OK)
  1636. {
  1637. while (sqlite3_step(statement) == SQLITE_ROW)
  1638. {
  1639. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1640. int _id = sqlite3_column_int(statement, 0);
  1641. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1642. NSString* nsimg_0=nil;
  1643. if(img_0!=nil)
  1644. {
  1645. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1646. [self copy_upImg:serial file:nsimg_0];
  1647. }
  1648. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1649. NSString* nsimg_1=nil;
  1650. if(img_1!=nil)
  1651. {
  1652. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1653. [self copy_upImg:serial file:nsimg_1];
  1654. }
  1655. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1656. NSString* nsimg_2=nil;
  1657. if(img_2!=nil)
  1658. {
  1659. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1660. [self copy_upImg:serial file:nsimg_2];
  1661. }
  1662. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1663. NSString* nssync_data=nil;
  1664. if(sync_data!=nil)
  1665. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1666. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1667. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1668. itemjson[@"json_data"]= nssync_data;
  1669. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1670. count++;
  1671. }
  1672. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1673. sqlite3_finalize(statement);
  1674. }
  1675. ret[@"count"]=[NSNumber numberWithInt:count ];
  1676. [iSalesDB close_db:db];
  1677. return ret;
  1678. }
  1679. +(bool) check_offlinedata
  1680. {
  1681. UIApplication * app = [UIApplication sharedApplication];
  1682. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1683. sqlite3 *db = [iSalesDB get_db];
  1684. NSString * where=@"1=1";
  1685. // if(appDelegate.user!=nil)
  1686. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1687. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1688. [iSalesDB close_db:db];
  1689. if(count==0)
  1690. {
  1691. return false;
  1692. }
  1693. return true;
  1694. //
  1695. //[iSalesDB close_db:db];
  1696. }
  1697. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1698. {
  1699. UIApplication * app = [UIApplication sharedApplication];
  1700. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1701. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1702. sqlite3 *db = [iSalesDB get_db];
  1703. NSString* collectId=params[@"collectId"];
  1704. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1705. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1706. [iSalesDB execSql:sqlQuery db:db];
  1707. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1708. [iSalesDB close_db:db];
  1709. appDelegate.wish_count =count;
  1710. [appDelegate update_count_mark];
  1711. ret[@"result"]= [NSNumber numberWithInt:2];
  1712. return ret;
  1713. }
  1714. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1715. //{
  1716. //
  1717. // UIApplication * app = [UIApplication sharedApplication];
  1718. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1719. //
  1720. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1721. // sqlite3 *db = [iSalesDB get_db];
  1722. // NSString* product_id=params[@"product_id"];
  1723. //
  1724. //
  1725. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1726. //
  1727. //
  1728. // for(int i=0;i<arr.count;i++)
  1729. // {
  1730. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1731. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1732. // if(count==0)
  1733. // {
  1734. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1735. // [iSalesDB execSql:sqlQuery db:db];
  1736. // }
  1737. // }
  1738. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1739. // [iSalesDB close_db:db];
  1740. //
  1741. // appDelegate.wish_count =count;
  1742. //
  1743. // [appDelegate update_count_mark];
  1744. // ret[@"result"]= [NSNumber numberWithInt:2];
  1745. // return ret;
  1746. // //
  1747. // //return ret;
  1748. //}
  1749. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1750. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1751. DebugLog(@"time interval: %lf",interval);
  1752. }
  1753. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1754. {
  1755. UIApplication * app = [UIApplication sharedApplication];
  1756. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1757. int sort = [[params objectForKey:@"sort"] intValue];
  1758. NSString *sort_str = @"";
  1759. switch (sort) {
  1760. case 0:{
  1761. sort_str = @"order by w.modify_time desc";
  1762. }
  1763. break;
  1764. case 1:{
  1765. sort_str = @"order by w.modify_time asc";
  1766. }
  1767. break;
  1768. case 2:{
  1769. sort_str = @"order by m.name asc";
  1770. }
  1771. break;
  1772. case 3:{
  1773. sort_str = @"order by m.name desc";
  1774. }
  1775. break;
  1776. case 4:{
  1777. sort_str = @"order by m.description asc";
  1778. }
  1779. break;
  1780. default:
  1781. break;
  1782. }
  1783. NSString* user = appDelegate.user;
  1784. sqlite3 *db = [iSalesDB get_db];
  1785. // order by w.create_time
  1786. NSString* sqlQuery = [NSString stringWithFormat:@"select w.product_id,m.name||'\n'||m.description,w._id,w.qty from wishlist as w left join product as m on w.product_id=m.product_id where w.is_delete is NULL or w.is_delete != 1 %@ ",sort_str];
  1787. // NSString* sqlQuery = [NSString stringWithFormat:@"select m.product_id,(m.name||'\n'||m.description) as des,w._id from product as m,wishlist as w where m.product_id = w.product_id %@",sort_str];
  1788. sqlite3_stmt * statement;
  1789. NSDate *date1 = [NSDate date];
  1790. NSDate *date2 = nil;
  1791. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1792. int count=0;
  1793. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1794. {
  1795. while (sqlite3_step(statement) == SQLITE_ROW)
  1796. {
  1797. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1798. int product_id = sqlite3_column_double(statement, 0);
  1799. char *description = (char*)sqlite3_column_text(statement, 1);
  1800. if(description==nil)
  1801. description= "";
  1802. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1803. int item_id = sqlite3_column_double(statement, 2);
  1804. NSDate *date_image = [NSDate date];
  1805. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1806. printf("image : ");
  1807. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1808. // char *url = (char*)sqlite3_column_text(statement, 3);
  1809. // if(url==nil)
  1810. // url="";
  1811. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1812. int qty = sqlite3_column_int(statement, 3);
  1813. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1814. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1815. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1816. item[@"description"]= nsdescription;
  1817. item[@"img"]= nsurl;
  1818. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1819. count++;
  1820. }
  1821. printf("total time:");
  1822. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1823. ret[@"count"]= [NSNumber numberWithInt:count];
  1824. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1825. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1826. ret[@"result"]= [NSNumber numberWithInt:2];
  1827. appDelegate.wish_count =count;
  1828. [appDelegate update_count_mark];
  1829. sqlite3_finalize(statement);
  1830. }
  1831. [iSalesDB close_db:db];
  1832. return ret;
  1833. }
  1834. +(NSDictionary*) offline_notimpl
  1835. {
  1836. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1837. ret[@"result"]=@"8";
  1838. ret[@"err_msg"]=@"offline mode does not support this function.";
  1839. return ret;
  1840. }
  1841. +(NSDictionary*) offline_home
  1842. {
  1843. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1844. NSString *cachefolder = [paths objectAtIndex:0];
  1845. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1846. NSData* json =nil;
  1847. json=[NSData dataWithContentsOfFile:img_cache];
  1848. NSError *error=nil;
  1849. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1850. return menu;
  1851. }
  1852. +(NSDictionary*) offline_category_menu
  1853. {
  1854. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1855. NSString *cachefolder = [paths objectAtIndex:0];
  1856. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1857. NSData* json =nil;
  1858. json=[NSData dataWithContentsOfFile:img_cache];
  1859. if(json==nil)
  1860. return nil;
  1861. NSError *error=nil;
  1862. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1863. return menu;
  1864. }
  1865. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1866. {
  1867. NSString* offline_command=params[@"offline_Command"];
  1868. NSDictionary* ret=nil;
  1869. if([offline_command isEqualToString:@"model_NIYMAL"])
  1870. {
  1871. NSString* category = params[@"category"];
  1872. ret = [self refresh_model_NIYMAL:category];
  1873. }
  1874. return ret;
  1875. }
  1876. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1877. {
  1878. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1879. [ret setValue:@"2" forKey:@"result"];
  1880. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1881. sqlite3* db= [iSalesDB get_db];
  1882. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1883. [iSalesDB close_db:db];
  1884. [ret setObject:detail1_section forKey:@"detail_1"];
  1885. return ret;
  1886. }
  1887. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1888. {
  1889. UIApplication * app = [UIApplication sharedApplication];
  1890. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1891. // NSArray* arr1 = [self get_user_all_price_type];
  1892. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1893. // NSSet *set1 = [NSSet setWithArray:arr1];
  1894. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1895. // if(appDelegate.contact_id==nil)
  1896. // set2=[set1 mutableCopy];
  1897. // else
  1898. // [set2 intersectsSet:set1];
  1899. // NSArray *retarr = [set2 allObjects];
  1900. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1901. // sqlite3 *db = [iSalesDB get_db];
  1902. NSString* sqlQuery = nil;
  1903. if(appDelegate.contact_id==nil)
  1904. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d order by a.order_by;",item_id];
  1905. else
  1906. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d and b.price_name in(%@) order by a.order_by;",item_id,whereprice];
  1907. sqlite3_stmt * statement;
  1908. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1909. int count=0;
  1910. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1911. {
  1912. while (sqlite3_step(statement) == SQLITE_ROW)
  1913. {
  1914. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1915. char *name = (char*)sqlite3_column_text(statement, 0);
  1916. if(name==nil)
  1917. name="";
  1918. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1919. // double price = sqlite3_column_double(statement, 1);
  1920. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1921. // if(isnull==nil)
  1922. // item[nsname]= @"No Price";
  1923. // else
  1924. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  1925. char *price = (char*)sqlite3_column_text(statement, 1);
  1926. if(price!=nil)
  1927. {
  1928. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1929. nsprice=[AESCrypt fastdecrypt:nsprice];
  1930. if(nsprice.length>0)
  1931. {
  1932. double dp= [nsprice doubleValue];
  1933. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  1934. }
  1935. }
  1936. else
  1937. {
  1938. item[nsname]= @"No Price";
  1939. }
  1940. // int type= sqlite3_column_int(statement, 2);
  1941. // item[@"type"]=[NSNumber numberWithInt:type ];
  1942. // item[nsname]= nsprice;
  1943. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1944. count++;
  1945. }
  1946. ret[@"count"]= [NSNumber numberWithInt:count];
  1947. sqlite3_finalize(statement);
  1948. }
  1949. // [iSalesDB close_db:db];
  1950. return ret;
  1951. }
  1952. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  1953. //{
  1954. // NSArray* arr1 = [self get_user_all_price_type:db];
  1955. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1956. //
  1957. // // NSSet *set1 = [NSSet setWithArray:arr1];
  1958. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1959. // // [set2 intersectsSet:set1];
  1960. // //
  1961. // //
  1962. // // NSArray *retarr = [set2 allObjects];
  1963. //
  1964. // NSString* whereprice=nil;
  1965. // if(contact_id==nil)
  1966. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1967. // else
  1968. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1969. //
  1970. //
  1971. // // sqlite3 *db = [iSalesDB get_db];
  1972. //
  1973. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  1974. // sqlite3_stmt * statement;
  1975. //
  1976. //
  1977. // NSNumber* ret = nil;
  1978. // double dprice=DBL_MAX;
  1979. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1980. // {
  1981. //
  1982. //
  1983. // while (sqlite3_step(statement) == SQLITE_ROW)
  1984. // {
  1985. //
  1986. // // double val = sqlite3_column_double(statement, 0);
  1987. // char *price = (char*)sqlite3_column_text(statement, 0);
  1988. // if(price!=nil)
  1989. // {
  1990. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1991. // nsprice=[AESCrypt fastdecrypt:nsprice];
  1992. // if(nsprice.length>0)
  1993. // {
  1994. // double dp= [nsprice doubleValue];
  1995. // if(dp<dprice)
  1996. // dprice=dp;
  1997. // }
  1998. // }
  1999. // }
  2000. //
  2001. //
  2002. //
  2003. //
  2004. // sqlite3_finalize(statement);
  2005. //
  2006. //
  2007. //
  2008. //
  2009. // }
  2010. //
  2011. // // [iSalesDB close_db:db];
  2012. //
  2013. // if(dprice==DBL_MAX)
  2014. // ret= nil;
  2015. // else
  2016. // ret= [NSNumber numberWithDouble:dprice];
  2017. // return ret;
  2018. //}
  2019. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2020. {
  2021. NSArray* arr1 = [self get_user_all_price_type:db];
  2022. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2023. NSString* whereprice=nil;
  2024. if(contact_id==nil)
  2025. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2026. else
  2027. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2028. // sqlite3 *db = [iSalesDB get_db];
  2029. NSString *productIdCondition = @"1 = 1";
  2030. if (product_id) {
  2031. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2032. }
  2033. NSString *itemIdCondition = @"";
  2034. if (item_id) {
  2035. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2036. }
  2037. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2038. sqlite3_stmt * statement;
  2039. NSNumber* ret = nil;
  2040. double dprice=DBL_MAX;
  2041. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2042. {
  2043. while (sqlite3_step(statement) == SQLITE_ROW)
  2044. {
  2045. // double val = sqlite3_column_double(statement, 0);
  2046. char *price = (char*)sqlite3_column_text(statement, 0);
  2047. if(price!=nil)
  2048. {
  2049. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2050. nsprice=[AESCrypt fastdecrypt:nsprice];
  2051. if(nsprice.length>0)
  2052. {
  2053. double dp= [nsprice doubleValue];
  2054. if(dp<dprice)
  2055. dprice=dp;
  2056. }
  2057. }
  2058. }
  2059. sqlite3_finalize(statement);
  2060. }
  2061. // [iSalesDB close_db:db];
  2062. if(dprice==DBL_MAX)
  2063. ret= nil;
  2064. else
  2065. ret= [NSNumber numberWithDouble:dprice];
  2066. return ret;
  2067. }
  2068. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  2069. {
  2070. NSArray* ret=nil;
  2071. // sqlite3 *db = [iSalesDB get_db];
  2072. // no customer assigned , use login user contact_id
  2073. UIApplication * app = [UIApplication sharedApplication];
  2074. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2075. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  2076. sqlite3_stmt * statement;
  2077. // int count=0;
  2078. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2079. {
  2080. if (sqlite3_step(statement) == SQLITE_ROW)
  2081. {
  2082. char *val = (char*)sqlite3_column_text(statement, 0);
  2083. if(val==nil)
  2084. val="";
  2085. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2086. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2087. }
  2088. sqlite3_finalize(statement);
  2089. }
  2090. // [iSalesDB close_db:db];
  2091. return ret;
  2092. }
  2093. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  2094. {
  2095. // sqlite3 *db = [iSalesDB get_db];
  2096. if(contact_id==nil)
  2097. {
  2098. // no customer assigned , use login user contact_id
  2099. UIApplication * app = [UIApplication sharedApplication];
  2100. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2101. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  2102. sqlite3_stmt * statement;
  2103. // int count=0;
  2104. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2105. {
  2106. if (sqlite3_step(statement) == SQLITE_ROW)
  2107. {
  2108. char *val = (char*)sqlite3_column_text(statement, 0);
  2109. if(val==nil)
  2110. val="";
  2111. contact_id = [[NSString alloc]initWithUTF8String:val];
  2112. }
  2113. sqlite3_finalize(statement);
  2114. }
  2115. if(contact_id.length<=0)
  2116. {
  2117. // [iSalesDB close_db:db];
  2118. return nil;
  2119. }
  2120. }
  2121. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2122. sqlite3_stmt * statement;
  2123. NSArray* ret=nil;
  2124. // int count=0;
  2125. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2126. {
  2127. if (sqlite3_step(statement) == SQLITE_ROW)
  2128. {
  2129. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2130. if(price_type==nil)
  2131. price_type="";
  2132. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2133. if(nsprice_type.length>0)
  2134. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2135. else
  2136. ret=nil;
  2137. }
  2138. sqlite3_finalize(statement);
  2139. }
  2140. // [iSalesDB close_db:db];
  2141. return ret;
  2142. }
  2143. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2144. {
  2145. NSString* ret= nil;
  2146. // sqlite3 *db = [iSalesDB get_db];
  2147. NSString *sqlQuery = nil;
  2148. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2149. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  2150. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  2151. if(product_id==nil && model_name)
  2152. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where product_id in (select product_id from product where name='%@') order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  2153. else if (product_id)
  2154. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where i.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  2155. sqlite3_stmt * statement;
  2156. // int count=0;
  2157. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2158. {
  2159. if (sqlite3_step(statement) == SQLITE_ROW)
  2160. {
  2161. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2162. if(imgurl==nil)
  2163. imgurl="";
  2164. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2165. ret=nsimgurl;
  2166. }
  2167. sqlite3_finalize(statement);
  2168. }
  2169. else
  2170. {
  2171. [ret setValue:@"8" forKey:@"result"];
  2172. }
  2173. // [iSalesDB close_db:db];
  2174. DebugLog(@"data string: %@",ret );
  2175. return ret;
  2176. }
  2177. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  2178. {
  2179. NSString* ret= nil;
  2180. sqlite3 *db = [iSalesDB get_db];
  2181. NSString *sqlQuery = nil;
  2182. if(product_id==nil)
  2183. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2184. else
  2185. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2186. sqlite3_stmt * statement;
  2187. // int count=0;
  2188. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2189. {
  2190. if (sqlite3_step(statement) == SQLITE_ROW)
  2191. {
  2192. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2193. if(default_category==nil)
  2194. default_category="";
  2195. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2196. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2197. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2198. ret=nsdefault_category;
  2199. }
  2200. sqlite3_finalize(statement);
  2201. }
  2202. else
  2203. {
  2204. [ret setValue:@"8" forKey:@"result"];
  2205. }
  2206. [iSalesDB close_db:db];
  2207. DebugLog(@"data string: %@",ret );
  2208. return ret;
  2209. }
  2210. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2211. {
  2212. NSString* model_name = [params valueForKey:@"product_name"];
  2213. NSString* product_id = [params valueForKey:@"product_id"];
  2214. NSString* category = [params valueForKey:@"category"];
  2215. if(category==nil)
  2216. category = [self model_default_category:product_id model_name:model_name];
  2217. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2218. sqlite3 *db = [iSalesDB get_db];
  2219. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2220. NSString *sqlQuery = nil;
  2221. if(product_id==nil)
  2222. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id from product where name='%@';",model_name]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where name='%@';
  2223. else
  2224. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id from product where product_id=%@;",product_id]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where product_id=%@;
  2225. sqlite3_stmt * statement;
  2226. [ret setValue:@"2" forKey:@"result"];
  2227. [ret setValue:@"3" forKey:@"detail_section_count"];
  2228. // int count=0;
  2229. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2230. {
  2231. if (sqlite3_step(statement) == SQLITE_ROW)
  2232. {
  2233. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2234. char *name = (char*)sqlite3_column_text(statement, 0);
  2235. if(name==nil)
  2236. name="";
  2237. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2238. char *description = (char*)sqlite3_column_text(statement, 1);
  2239. if(description==nil)
  2240. description="";
  2241. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2242. int product_id = sqlite3_column_int(statement, 2);
  2243. char *color = (char*)sqlite3_column_text(statement, 3);
  2244. if(color==nil)
  2245. color="";
  2246. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2247. //
  2248. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2249. // if(legcolor==nil)
  2250. // legcolor="";
  2251. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2252. //
  2253. //
  2254. int availability = sqlite3_column_int(statement, 5);
  2255. //
  2256. int incoming_stock = sqlite3_column_int(statement, 6);
  2257. char *demension = (char*)sqlite3_column_text(statement, 7);
  2258. if(demension==nil)
  2259. demension="";
  2260. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2261. // ,,,,,,,,,
  2262. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2263. if(seat_height==nil)
  2264. seat_height="";
  2265. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2266. char *material = (char*)sqlite3_column_text(statement, 9);
  2267. if(material==nil)
  2268. material="";
  2269. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2270. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2271. if(box_dim==nil)
  2272. box_dim="";
  2273. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2274. char *volume = (char*)sqlite3_column_text(statement, 11);
  2275. if(volume==nil)
  2276. volume="";
  2277. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2278. double weight = sqlite3_column_double(statement, 12);
  2279. char *model_set = (char*)sqlite3_column_text(statement, 13);
  2280. if(model_set==nil)
  2281. model_set="";
  2282. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2283. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2284. if(load_ability==nil)
  2285. load_ability="";
  2286. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2287. char *default_category = (char*)sqlite3_column_text(statement, 15);
  2288. if(default_category==nil)
  2289. default_category="";
  2290. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2291. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2292. if(fabric_content==nil)
  2293. fabric_content="";
  2294. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2295. char *assembling = (char*)sqlite3_column_text(statement, 17);
  2296. if(assembling==nil)
  2297. assembling="";
  2298. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2299. char *made_in = (char*)sqlite3_column_text(statement, 18);
  2300. if(made_in==nil)
  2301. made_in="";
  2302. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2303. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2304. if(special_remarks==nil)
  2305. special_remarks="";
  2306. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2307. int stockUcom = sqlite3_column_double(statement, 20);
  2308. char *product_group = (char*)sqlite3_column_text(statement, 21);
  2309. if(product_group==nil)
  2310. product_group="";
  2311. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2312. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2313. // if(fashion_selector==nil)
  2314. // fashion_selector="";
  2315. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2316. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2317. if(selector_field==nil)
  2318. selector_field="";
  2319. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2320. char *property_field = (char*)sqlite3_column_text(statement, 23);
  2321. if(property_field==nil)
  2322. property_field="";
  2323. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2324. char *packaging = (char*)sqlite3_column_text(statement, 24);
  2325. if(packaging==nil)
  2326. packaging="";
  2327. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2328. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2329. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2330. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2331. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2332. [img_section setValue:model_s_img forKey:@"model_s_img"];
  2333. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2334. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2335. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2336. NSString* Availability=nil;
  2337. if(availability>0)
  2338. Availability=[NSString stringWithFormat:@"%d",availability];
  2339. else
  2340. Availability = @"In Production";
  2341. [img_section setValue:Availability forKey:@"Availability"];
  2342. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2343. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2344. char *eta = (char*)sqlite3_column_text(statement, 25);
  2345. if(eta==nil)
  2346. eta="";
  2347. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2348. [img_section setValue:nseta forKey:@"ETA"];
  2349. int item_id = sqlite3_column_int(statement, 26);
  2350. NSString* Price=nil;
  2351. if(appDelegate.bLogin==false)
  2352. Price=@"Must Sign in.";
  2353. else
  2354. {
  2355. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2356. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2357. if(price==nil)
  2358. Price=@"No Price.";
  2359. else
  2360. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2361. }
  2362. [img_section setValue:Price forKey:@"price"];
  2363. [img_section setValue:nsname forKey:@"model_name"];
  2364. [img_section setValue:nsdescription forKey:@"model_descrition"];
  2365. [ret setObject:img_section forKey:@"img_section"];
  2366. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2367. int detail0_item_count=0;
  2368. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2369. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2370. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2371. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2372. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2373. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2374. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2375. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2376. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2377. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2378. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2379. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2380. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2381. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2382. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2383. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2384. {
  2385. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2386. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2387. }
  2388. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2389. [detail0_section setValue:@"kv" forKey:@"type"];
  2390. [detail0_section setValue:@"Product Information" forKey:@"title"];
  2391. [ret setObject:detail0_section forKey:@"detail_0"];
  2392. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2393. // [detail1_section setValue:@"detail" forKey:@"target"];
  2394. // [detail1_section setValue:@"popup" forKey:@"action"];
  2395. // [detail1_section setValue:@"content" forKey:@"type"];
  2396. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2397. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2398. // [detail1_section setValue:@"true" forKey:@"single_row"];
  2399. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2400. [ret setObject:detail1_section forKey:@"detail_1"];
  2401. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2402. [detail2_section setValue:@"detail" forKey:@"target"];
  2403. [detail2_section setValue:@"popup" forKey:@"action"];
  2404. [detail2_section setValue:@"content" forKey:@"type"];
  2405. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2406. [detail2_section setValue:@"true" forKey:@"single_row"];
  2407. [detail2_section setValue:@"local" forKey:@"data"];
  2408. [ret setObject:detail2_section forKey:@"detail_2"];
  2409. }
  2410. sqlite3_finalize(statement);
  2411. }
  2412. else
  2413. {
  2414. [ret setValue:@"8" forKey:@"result"];
  2415. }
  2416. DebugLog(@"count:%d",count);
  2417. [iSalesDB close_db:db];
  2418. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2419. return ret;
  2420. }
  2421. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2422. NSString* orderCode = [params valueForKey:@"orderCode"];
  2423. NSString* keyword = [params valueForKey:@"keyword"];
  2424. keyword=keyword.lowercaseString;
  2425. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  2426. int limit = [[params valueForKey:@"limit"] intValue];
  2427. int offset = [[params valueForKey:@"offset"] intValue];
  2428. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2429. NSString *limit_str = @"";
  2430. if (limited) {
  2431. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2432. }
  2433. sqlite3 *db = [iSalesDB get_db];
  2434. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2435. UIApplication * app = [UIApplication sharedApplication];
  2436. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2437. NSString *sqlQuery = nil;
  2438. if(exactMatch )
  2439. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where lower(name) like'%@%%' and m.is_active = 1 order by m.name %@;",appDelegate.order_code, keyword,limit_str]; // select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%@%%' order by m.name limit %d offset %d ;
  2440. else
  2441. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where (lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, keyword,keyword,limit_str];// select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%%%@%%' or
  2442. DebugLog(@"offline_search sql:%@",sqlQuery);
  2443. sqlite3_stmt * statement;
  2444. [ret setValue:@"2" forKey:@"result"];
  2445. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2446. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2447. // int count=0;
  2448. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2449. {
  2450. int i=0;
  2451. while (sqlite3_step(statement) == SQLITE_ROW)
  2452. {
  2453. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2454. // char *name = (char*)sqlite3_column_text(statement, 1);
  2455. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2456. char *name = (char*)sqlite3_column_text(statement, 0);
  2457. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2458. char *description = (char*)sqlite3_column_text(statement, 1);
  2459. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2460. int product_id = sqlite3_column_int(statement, 2);
  2461. // char *url = (char*)sqlite3_column_text(statement, 3);
  2462. // if(url==nil)
  2463. // url="";
  2464. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2465. int wid = sqlite3_column_int(statement, 3);
  2466. int closeout = sqlite3_column_int(statement, 4);
  2467. int cid = sqlite3_column_int(statement, 5);
  2468. int wisdelete = sqlite3_column_int(statement, 6);
  2469. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2470. if(wid !=0 && wisdelete != 1)
  2471. [item setValue:@"true" forKey:@"wish_exists"];
  2472. else
  2473. [item setValue:@"false" forKey:@"wish_exists"];
  2474. if(closeout==0)
  2475. [item setValue:@"false" forKey:@"is_closeout"];
  2476. else
  2477. [item setValue:@"true" forKey:@"is_closeout"];
  2478. if(cid==0)
  2479. [item setValue:@"false" forKey:@"cart_exists"];
  2480. else
  2481. [item setValue:@"true" forKey:@"cart_exists"];
  2482. [item addEntriesFromDictionary:imgjson];
  2483. // [item setValue:nsurl forKey:@"img"];
  2484. [item setValue:nsname forKey:@"fash_name"];
  2485. [item setValue:nsdescription forKey:@"description"];
  2486. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2487. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2488. i++;
  2489. }
  2490. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2491. [ret setObject:items forKey:@"items"];
  2492. sqlite3_finalize(statement);
  2493. }
  2494. DebugLog(@"count:%d",count);
  2495. [iSalesDB close_db:db];
  2496. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2497. return ret;
  2498. }
  2499. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2500. {
  2501. return [self search:params limited:YES];
  2502. }
  2503. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2504. {
  2505. UIApplication * app = [UIApplication sharedApplication];
  2506. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2507. appDelegate.disable_trigger=true;
  2508. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2509. appDelegate.disable_trigger=false;
  2510. //
  2511. // NSString* user = [params valueForKey:@"user"];
  2512. //
  2513. // NSString* password = [params valueForKey:@"password"];
  2514. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2515. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2516. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2517. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2518. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2519. [appDelegate update_count_mark];
  2520. appDelegate.can_show_price =false;
  2521. appDelegate.can_see_price =false;
  2522. appDelegate.can_create_portfolio =false;
  2523. appDelegate.can_create_order =false;
  2524. appDelegate.can_cancel_order =false;
  2525. appDelegate.can_set_cart_price =false;
  2526. appDelegate.can_delete_order =false;
  2527. appDelegate.can_submit_order =false;
  2528. appDelegate.can_set_tearsheet_price =false;
  2529. appDelegate.can_update_contact_info = false;
  2530. appDelegate.save_order_logout = false;
  2531. appDelegate.submit_order_logout = false;
  2532. appDelegate.alert_sold_in_quantities = false;
  2533. appDelegate.ipad_perm =nil ;
  2534. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2535. appDelegate.OrderFilter= nil;
  2536. [appDelegate SetSo:nil];
  2537. [appDelegate set_main_button_panel];
  2538. // sqlite3 *db = [iSalesDB get_db];
  2539. //
  2540. //
  2541. //
  2542. //
  2543. //
  2544. // NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  2545. //
  2546. //
  2547. //
  2548. //
  2549. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2550. // sqlite3_stmt * statement;
  2551. //
  2552. //
  2553. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2554. //
  2555. //
  2556. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2557. // {
  2558. //
  2559. //
  2560. // if (sqlite3_step(statement) == SQLITE_ROW)
  2561. // {
  2562. //
  2563. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2564. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2565. //
  2566. //
  2567. //
  2568. // int can_show_price = sqlite3_column_int(statement, 0);
  2569. // int can_see_price = sqlite3_column_int(statement, 1);
  2570. //
  2571. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2572. // if(contact_id==nil)
  2573. // contact_id="";
  2574. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2575. //
  2576. // int user_type = sqlite3_column_int(statement, 3);
  2577. //
  2578. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2579. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2580. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2581. // int can_delete_order = sqlite3_column_int(statement, 7);
  2582. // int can_submit_order = sqlite3_column_int(statement, 8);
  2583. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2584. // int can_create_order = sqlite3_column_int(statement, 10);
  2585. //
  2586. //
  2587. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2588. // if(mode==nil)
  2589. // mode="";
  2590. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2591. //
  2592. //
  2593. // char *username = (char*)sqlite3_column_text(statement, 12);
  2594. // if(username==nil)
  2595. // username="";
  2596. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2597. //
  2598. //
  2599. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2600. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2601. // [header setValue:nscontact_id forKey:@"contact_id"];
  2602. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2603. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2604. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2605. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2606. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2607. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2608. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2609. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2610. //
  2611. // [header setValue:nsusername forKey:@"username"];
  2612. //
  2613. //
  2614. // [ret setObject:header forKey:@"header"];
  2615. // [ret setValue:nsmode forKey:@"mode"];
  2616. //
  2617. //
  2618. // }
  2619. //
  2620. //
  2621. //
  2622. // sqlite3_finalize(statement);
  2623. // }
  2624. //
  2625. //
  2626. //
  2627. // [iSalesDB close_db:db];
  2628. //
  2629. //
  2630. //
  2631. //
  2632. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2633. return ret;
  2634. }
  2635. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2636. {
  2637. UIApplication * app = [UIApplication sharedApplication];
  2638. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2639. appDelegate.disable_trigger=true;
  2640. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2641. appDelegate.disable_trigger=false;
  2642. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2643. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2644. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2645. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2646. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2647. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2648. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2649. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2650. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2651. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2652. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2653. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2654. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2655. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2656. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2657. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2658. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2659. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2660. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2661. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2662. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2663. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2664. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2665. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2666. [arr_name addObject:customer_first_name];
  2667. [arr_name addObject:customer_last_name];
  2668. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2669. // default ship from
  2670. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,decrypt(phone) from offline_contact where contact_id = 'NPD';";
  2671. __block NSString *cid = @"";
  2672. __block NSString *name = @"";
  2673. __block NSString *ext = @"";
  2674. __block NSString *contact = @"";
  2675. __block NSString *email = @"";
  2676. __block NSString *fax = @"";
  2677. __block NSString *phone = @"";
  2678. sqlite3 *db = [iSalesDB get_db];
  2679. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2680. cid = [self textAtColumn:0 statement:statment];
  2681. name = [self textAtColumn:1 statement:statment];
  2682. ext = [self textAtColumn:2 statement:statment];
  2683. contact = [self textAtColumn:3 statement:statment];
  2684. email = [self textAtColumn:4 statement:statment];
  2685. fax = [self textAtColumn:5 statement:statment];
  2686. phone = [self textAtColumn:6 statement:statment];
  2687. }];
  2688. NSString* so_id = [self get_offline_soid:db];
  2689. if(so_id==nil)
  2690. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2691. NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact,must_call,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,sales_terms) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','FOB')",so_id,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name,cid,name,ext,contact,phone,fax,email,customer_cid,customer_name,customer_contact_ext,customer_contact,customer_phone,customer_fax,customer_email];
  2692. int result =[iSalesDB execSql:sql_neworder db:db];
  2693. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2694. //insert into offline_order(so_id,status,contact_id,sales_rep,create_by,customer_contact) select 'OFFLINE'||hex(randomblob(16)),-1,contact_id,sales_rep,company_name from offline_contact where contact_id='99FURO0002'
  2695. //soId
  2696. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2697. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2698. sqlite3_stmt * statement;
  2699. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2700. {
  2701. if (sqlite3_step(statement) == SQLITE_ROW)
  2702. {
  2703. // char *name = (char*)sqlite3_column_text(statement, 1);
  2704. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2705. //ret = sqlite3_column_int(statement, 0);
  2706. char *soId = (char*)sqlite3_column_text(statement, 0);
  2707. if(soId==nil)
  2708. soId="";
  2709. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2710. [ret setValue:nssoId forKey:@"soId"];
  2711. [ret setValue:nssoId forKey:@"orderCode"];
  2712. }
  2713. sqlite3_finalize(statement);
  2714. }
  2715. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2716. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2717. [iSalesDB close_db:db];
  2718. return [RAUtils dict2data:ret];
  2719. }
  2720. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2721. {
  2722. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2723. NSString* orderCode = [params valueForKey:@"orderCode"];
  2724. UIApplication * app = [UIApplication sharedApplication];
  2725. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2726. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2727. {
  2728. appDelegate.disable_trigger=true;
  2729. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2730. appDelegate.disable_trigger=false;
  2731. }
  2732. sqlite3 *db = [iSalesDB get_db];
  2733. int cart_count=[self query_ordercartcount:orderCode db:db];
  2734. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2735. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2736. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2737. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2738. [iSalesDB close_db:db];
  2739. return [RAUtils dict2data:ret];
  2740. }
  2741. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2742. {
  2743. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2744. sqlite3 *db = [iSalesDB get_db];
  2745. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2746. int count =0;
  2747. if(params[@"count"]!=nil)
  2748. {
  2749. count = [params[@"count"] intValue];
  2750. }
  2751. NSString* product_id=params[@"product_id"];
  2752. NSString* orderCode=params[@"orderCode"];
  2753. NSString *qty = params[@"qty"];
  2754. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2755. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2756. for(int i=0;i<arr_id.count;i++)
  2757. {
  2758. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2759. int item_qty= count;
  2760. if (qty) {
  2761. item_qty = [qty_arr[i] integerValue];
  2762. }
  2763. if(item_qty==0)
  2764. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2765. NSString* sql=nil;
  2766. sqlite3_stmt *stmt;
  2767. if(_id<0)
  2768. {
  2769. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2770. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2771. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2772. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  2773. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  2774. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2775. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2776. }
  2777. else
  2778. {
  2779. if (qty) {
  2780. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2781. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2782. sqlite3_bind_int(stmt, 1, _id);
  2783. } else {
  2784. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2785. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2786. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2787. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2788. }
  2789. }
  2790. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2791. [iSalesDB execSql:@"ROLLBACK" db:db];
  2792. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2793. [iSalesDB close_db:db];
  2794. DebugLog(@"add to cart error");
  2795. return [RAUtils dict2data:ret];
  2796. }
  2797. }
  2798. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2799. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2800. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2801. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2802. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2803. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2804. [iSalesDB close_db:db];
  2805. return [RAUtils dict2data:ret];
  2806. }
  2807. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute
  2808. {
  2809. UIApplication * app = [UIApplication sharedApplication];
  2810. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2811. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2812. NSString *sqlQuery = [NSString stringWithFormat:@"select b.item_id,b.qty,c.name,c.description,b.unit_price,b.use_unitprice from (select item_id,qty,decrypt(unit_price) as unit_price,use_unitprice from bundle where product_item_id=%d) b left join catalog c on b.item_id=c.item_id;",item_id];
  2813. sqlite3_stmt * statement;
  2814. int count=0;
  2815. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2816. {
  2817. while (sqlite3_step(statement) == SQLITE_ROW)
  2818. {
  2819. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  2820. int bitem_id = sqlite3_column_int(statement, 0);
  2821. int bitem_qty = sqlite3_column_int(statement, 1);
  2822. char *name = (char*)sqlite3_column_text(statement, 2);
  2823. if(name==nil)
  2824. name="";
  2825. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  2826. char *description = (char*)sqlite3_column_text(statement, 3);
  2827. if(description==nil)
  2828. description="";
  2829. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  2830. double unit_price = sqlite3_column_double(statement, 4);
  2831. int use_unitprice = sqlite3_column_int(statement, 5);
  2832. if(use_unitprice!=1)
  2833. {
  2834. unit_price=[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db].doubleValue;
  2835. }
  2836. itemjson[@"model"]=nsname;
  2837. itemjson[@"description"]=nsdescription;
  2838. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  2839. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  2840. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  2841. if(compute)
  2842. {
  2843. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute];
  2844. }
  2845. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2846. count++;
  2847. }
  2848. sqlite3_finalize(statement);
  2849. }
  2850. ret[@"count"]=@(count);
  2851. if(count==0)
  2852. return nil;
  2853. else
  2854. return ret;
  2855. }
  2856. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  2857. {
  2858. //compute: add part to subtotal;
  2859. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  2860. dict_item[@(item_id)]=@"1";
  2861. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2862. double cuft=0;
  2863. double weight=0;
  2864. int carton=0;
  2865. NSString *sqlQuery = [NSString stringWithFormat:@"select ulength ,uwidth ,uheight ,uweight ,mlength ,mwidth ,mheight ,mweigth ,ilength ,iwidth ,iheight , iweight ,pcs ,mpack ,ipack,ucbf,icbf,mcbf from catalog where item_id=%d;",item_id];
  2866. sqlite3_stmt * statement;
  2867. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2868. {
  2869. if (sqlite3_step(statement) == SQLITE_ROW)
  2870. {
  2871. double ulength = sqlite3_column_double(statement, 0);
  2872. double uwidth = sqlite3_column_double(statement, 1);
  2873. double uheight = sqlite3_column_double(statement, 2);
  2874. double uweight = sqlite3_column_double(statement, 3);
  2875. double mlength = sqlite3_column_double(statement, 4);
  2876. double mwidth = sqlite3_column_double(statement, 5);
  2877. double mheight = sqlite3_column_double(statement, 6);
  2878. double mweight = sqlite3_column_double(statement, 7);
  2879. double ilength = sqlite3_column_double(statement, 8);
  2880. double iwidth = sqlite3_column_double(statement, 9);
  2881. double iheight = sqlite3_column_double(statement, 10);
  2882. double iweight = sqlite3_column_double(statement, 11);
  2883. int pcs = sqlite3_column_int(statement,12);
  2884. int mpack = sqlite3_column_int(statement, 13);
  2885. int ipack = sqlite3_column_int(statement, 14);
  2886. double ucbf = sqlite3_column_double(statement, 15);
  2887. double icbf = sqlite3_column_double(statement, 16);
  2888. double mcbf = sqlite3_column_double(statement, 17);
  2889. if(ipack==0)
  2890. {
  2891. carton= count/mpack ;
  2892. weight = mweight*carton;
  2893. cuft= carton*(mlength*mwidth*mheight);
  2894. int remain=count%mpack;
  2895. if(remain==0)
  2896. {
  2897. //do nothing;
  2898. }
  2899. else
  2900. {
  2901. carton++;
  2902. weight += uweight*remain;
  2903. cuft += (ulength*uwidth*uheight)*remain;
  2904. }
  2905. }
  2906. else
  2907. {
  2908. carton = count/(mpack*ipack);
  2909. weight = mweight*carton;
  2910. cuft= carton*(mlength*mwidth*mheight);
  2911. int remain=count%(mpack*ipack);
  2912. if(remain==0)
  2913. {
  2914. // do nothing;
  2915. }
  2916. else
  2917. {
  2918. carton++;
  2919. int icarton =remain/ipack;
  2920. int iremain=remain%ipack;
  2921. weight += iweight*icarton;
  2922. cuft += (ilength*iwidth*iheight)*icarton;
  2923. if(iremain==0)
  2924. {
  2925. //do nothing;
  2926. }
  2927. else
  2928. {
  2929. weight += uweight*iremain;
  2930. cuft += (ulength*uwidth*uheight)*iremain;
  2931. }
  2932. }
  2933. }
  2934. #ifdef BUILD_NPD
  2935. cuft=ucbf*count;
  2936. weight= uweight*count;
  2937. #endif
  2938. }
  2939. sqlite3_finalize(statement);
  2940. }
  2941. if(compute)
  2942. {
  2943. NSArray * arr_count=nil;
  2944. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  2945. for(int i=0;i<arr_bundle.count;i++)
  2946. {
  2947. dict_item[arr_bundle[i]]=@"1";
  2948. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  2949. cuft+=[bundlejson[@"cuft"] doubleValue];
  2950. weight+=[bundlejson[@"weight"] doubleValue];
  2951. carton+=[bundlejson[@"carton"] intValue];
  2952. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  2953. }
  2954. }
  2955. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  2956. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  2957. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  2958. ret[@"items"]=dict_item;
  2959. return ret;
  2960. }
  2961. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  2962. {
  2963. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  2964. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  2965. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2966. DebugLog(@"offline_login sql:%@",sqlQuery);
  2967. sqlite3_stmt * statement;
  2968. int cart_count=0;
  2969. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2970. if ( dbresult== SQLITE_OK)
  2971. {
  2972. while (sqlite3_step(statement) == SQLITE_ROW)
  2973. {
  2974. int item_id = sqlite3_column_int(statement, 0);
  2975. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  2976. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2977. }
  2978. sqlite3_finalize(statement);
  2979. }
  2980. return cart_count;
  2981. }
  2982. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  2983. {
  2984. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2985. sqlite3 *db = [iSalesDB get_db];
  2986. UIApplication * app = [UIApplication sharedApplication];
  2987. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2988. NSString* orderCode=params[@"orderCode"];
  2989. int sort = [[params objectForKey:@"sort"] intValue];
  2990. NSString *sort_str = @"";
  2991. switch (sort) {
  2992. case 0:{
  2993. sort_str = @"order by c.modify_time desc";
  2994. }
  2995. break;
  2996. case 1:{
  2997. sort_str = @"order by c.modify_time asc";
  2998. }
  2999. break;
  3000. case 2:{
  3001. sort_str = @"order by m.name asc";
  3002. }
  3003. break;
  3004. case 3:{
  3005. sort_str = @"order by m.name desc";
  3006. }
  3007. break;
  3008. case 4:{
  3009. sort_str = @"order by m.description asc";
  3010. }
  3011. break;
  3012. default:
  3013. break;
  3014. }
  3015. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id %@",orderCode,sort_str ];
  3016. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  3017. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3018. DebugLog(@"offline_login sql:%@",sqlQuery);
  3019. sqlite3_stmt * statement;
  3020. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3021. NSDate *date1 = [NSDate date];
  3022. int count=0;
  3023. int cart_count=0;
  3024. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3025. if ( dbresult== SQLITE_OK)
  3026. {
  3027. while (sqlite3_step(statement) == SQLITE_ROW)
  3028. {
  3029. NSDate *row_date = [NSDate date];
  3030. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3031. int product_id = sqlite3_column_int(statement, 0);
  3032. char *str_price = (char*)sqlite3_column_text(statement, 1);
  3033. int item_id = sqlite3_column_int(statement, 7);
  3034. NSString* Price=nil;
  3035. if(str_price==nil)
  3036. {
  3037. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3038. // NSDate *price_date = [NSDate date];
  3039. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3040. // DebugLog(@"price time interval");
  3041. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3042. if(price==nil)
  3043. Price=@"No Price.";
  3044. else
  3045. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3046. }
  3047. else
  3048. {
  3049. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3050. }
  3051. double discount = sqlite3_column_double(statement, 2);
  3052. int item_count = sqlite3_column_int(statement, 3);
  3053. char *line_note = (char*)sqlite3_column_text(statement, 4);
  3054. NSString *nsline_note=nil;
  3055. if(line_note!=nil)
  3056. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3057. char *name = (char*)sqlite3_column_text(statement, 5);
  3058. NSString *nsname=nil;
  3059. if(name!=nil)
  3060. nsname= [[NSString alloc]initWithUTF8String:name];
  3061. char *description = (char*)sqlite3_column_text(statement, 6);
  3062. NSString *nsdescription=nil;
  3063. if(description!=nil)
  3064. nsdescription= [[NSString alloc]initWithUTF8String:description];
  3065. int stockUom = sqlite3_column_int(statement, 8);
  3066. int _id = sqlite3_column_int(statement, 9);
  3067. // NSDate *subtotal_date = [NSDate date];
  3068. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3069. // DebugLog(@"subtotal_date time interval");
  3070. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3071. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3072. double weight=[bsubtotaljson[@"weight"] doubleValue];
  3073. int carton=[bsubtotaljson[@"carton"] intValue];
  3074. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3075. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3076. // NSDate *img_date = [NSDate date];
  3077. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3078. // DebugLog(@"img_date time interval");
  3079. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3080. itemjson[@"model"]=nsname;
  3081. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3082. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3083. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3084. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3085. itemjson[@"check"]=@"true";
  3086. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3087. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3088. itemjson[@"unit_price"]=Price;
  3089. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3090. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3091. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3092. itemjson[@"note"]=nsline_note;
  3093. // NSDate *date2 = [NSDate date];
  3094. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  3095. // DebugLog(@"model_bundle time interval");
  3096. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3097. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3098. count++;
  3099. // DebugLog(@"row time interval");
  3100. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3101. }
  3102. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3103. sqlite3_finalize(statement);
  3104. }
  3105. DebugLog(@"request cart total time interval");
  3106. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3107. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3108. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3109. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3110. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3111. ret[@"count"]=[NSNumber numberWithInt:count ];
  3112. ret[@"mode"]=@"Regular Mode";
  3113. [iSalesDB close_db:db];
  3114. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3115. DebugLog(@"general notes :%@",general_note);
  3116. ret[@"general_note"]= general_note;
  3117. return [RAUtils dict2data:ret];
  3118. }
  3119. +(NSData*) offline_login :(NSMutableDictionary *) params
  3120. {
  3121. NSString* user = [params valueForKey:@"user"];
  3122. NSString* password = [params valueForKey:@"password"];
  3123. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3124. sqlite3 *db = [iSalesDB get_db];
  3125. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  3126. DebugLog(@"offline_login sql:%@",sqlQuery);
  3127. sqlite3_stmt * statement;
  3128. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3129. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3130. {
  3131. if (sqlite3_step(statement) == SQLITE_ROW)
  3132. {
  3133. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3134. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3135. int can_show_price = sqlite3_column_int(statement, 0);
  3136. int can_see_price = sqlite3_column_int(statement, 1);
  3137. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3138. if(contact_id==nil)
  3139. contact_id="";
  3140. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3141. int user_type = sqlite3_column_int(statement, 3);
  3142. int can_cancel_order = sqlite3_column_int(statement, 4);
  3143. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3144. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3145. int can_delete_order = sqlite3_column_int(statement, 7);
  3146. int can_submit_order = sqlite3_column_int(statement, 8);
  3147. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3148. int can_create_order = sqlite3_column_int(statement, 10);
  3149. char *mode = (char*)sqlite3_column_text(statement, 11);
  3150. if(mode==nil)
  3151. mode="";
  3152. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3153. char *username = (char*)sqlite3_column_text(statement, 12);
  3154. if(username==nil)
  3155. username="";
  3156. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3157. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3158. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3159. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3160. [header setValue:nscontact_id forKey:@"contact_id"];
  3161. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3162. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3163. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3164. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3165. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3166. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3167. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3168. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3169. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3170. [header setValue:nsusername forKey:@"username"];
  3171. NSError* error=nil;
  3172. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3173. [header setValue:statusFilter forKey:@"statusFilter"];
  3174. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3175. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3176. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3177. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3178. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3179. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3180. [ret setObject:header forKey:@"header"];
  3181. [ret setValue:nsmode forKey:@"mode"];
  3182. }
  3183. sqlite3_finalize(statement);
  3184. }
  3185. [iSalesDB close_db:db];
  3186. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3187. return [RAUtils dict2data:ret];
  3188. }
  3189. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3190. {
  3191. NSString* contactId = [params valueForKey:@"contactId"];
  3192. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3193. sqlite3 *db = [iSalesDB get_db];
  3194. NSString *sqlQuery = nil;
  3195. {
  3196. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email,img_0,img_1,img_2,price_type,notes,sales_rep,type from offline_contact where contact_id='%@'",contactId];
  3197. }
  3198. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3199. sqlite3_stmt * statement;
  3200. [ret setValue:@"2" forKey:@"result"];
  3201. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3202. {
  3203. //int i = 0;
  3204. if (sqlite3_step(statement) == SQLITE_ROW)
  3205. {
  3206. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3207. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3208. // int editable = sqlite3_column_int(statement, 0);
  3209. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3210. NSString *nscompany_name =nil;
  3211. if(company_name==nil)
  3212. nscompany_name=@"";
  3213. else
  3214. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3215. char *country = (char*)sqlite3_column_text(statement, 2);
  3216. if(country==nil)
  3217. country="";
  3218. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3219. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3220. // if(addr==nil)
  3221. // addr="";
  3222. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3223. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3224. if(zipcode==nil)
  3225. zipcode="";
  3226. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3227. char *state = (char*)sqlite3_column_text(statement, 5);
  3228. if(state==nil)
  3229. state="";
  3230. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3231. char *city = (char*)sqlite3_column_text(statement, 6);
  3232. if(city==nil)
  3233. city="";
  3234. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3235. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3236. // NSString *nscontact_name = nil;
  3237. // if(contact_name==nil)
  3238. // nscontact_name=@"";
  3239. // else
  3240. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3241. char *phone = (char*)sqlite3_column_text(statement, 8);
  3242. NSString *nsphone = nil;
  3243. if(phone==nil)
  3244. nsphone=@"";
  3245. else
  3246. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3247. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3248. if(contact_id==nil)
  3249. contact_id="";
  3250. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3251. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3252. if(addr_1==nil)
  3253. addr_1="";
  3254. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3255. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3256. if(addr_2==nil)
  3257. addr_2="";
  3258. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3259. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3260. if(addr_3==nil)
  3261. addr_3="";
  3262. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3263. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3264. if(addr_4==nil)
  3265. addr_4="";
  3266. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3267. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3268. if(first_name==nil)
  3269. first_name="";
  3270. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3271. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3272. if(last_name==nil)
  3273. last_name="";
  3274. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3275. char *fax = (char*)sqlite3_column_text(statement, 16);
  3276. NSString *nsfax = nil;
  3277. if(fax==nil)
  3278. nsfax=@"";
  3279. else
  3280. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3281. char *email = (char*)sqlite3_column_text(statement, 17);
  3282. NSString *nsemail = nil;
  3283. if(email==nil)
  3284. nsemail=@"";
  3285. else
  3286. nsemail= [[NSString alloc]initWithUTF8String:email];
  3287. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3288. NSString *nsimg_0 = nil;
  3289. if(img_0==nil)
  3290. nsimg_0=@"";
  3291. else
  3292. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3293. [self copy_bcardImg:nsimg_0];
  3294. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3295. NSString *nsimg_1 = nil;
  3296. if(img_1==nil)
  3297. nsimg_1=@"";
  3298. else
  3299. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3300. [self copy_bcardImg:nsimg_1];
  3301. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3302. NSString *nsimg_2 = nil;
  3303. if(img_2==nil)
  3304. nsimg_2=@"";
  3305. else
  3306. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3307. [self copy_bcardImg:nsimg_2];
  3308. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3309. NSString *nsprice_type = nil;
  3310. if(price_type==nil)
  3311. nsprice_type=@"";
  3312. else
  3313. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3314. char *notes = (char*)sqlite3_column_text(statement, 22);
  3315. NSString *nsnotes = nil;
  3316. if(notes==nil)
  3317. nsnotes=@"";
  3318. else
  3319. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3320. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3321. NSString *nssalesrep = nil;
  3322. if(salesrep==nil)
  3323. nssalesrep=@"";
  3324. else
  3325. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3326. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3327. {
  3328. // decrypt
  3329. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3330. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3331. nsphone=[AESCrypt fastdecrypt:nsphone];
  3332. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3333. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3334. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3335. }
  3336. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3337. [arr_name addObject:nsfirst_name];
  3338. [arr_name addObject:nslast_name];
  3339. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3340. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3341. [arr_ext addObject:nsaddr_1];
  3342. [arr_ext addObject:nsaddr_2];
  3343. [arr_ext addObject:nsaddr_3];
  3344. [arr_ext addObject:nsaddr_4];
  3345. [arr_ext addObject:@"\r\n"];
  3346. [arr_ext addObject:nscity];
  3347. [arr_ext addObject:nsstate];
  3348. [arr_ext addObject:nszipcode];
  3349. [arr_ext addObject:nscountry];
  3350. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  3351. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3352. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3353. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3354. [item setValue:nscountry forKey:@"customer_country"];
  3355. [item setValue:nsphone forKey:@"customer_phone"];
  3356. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3357. [item setValue:nscompany_name forKey:@"customer_name"];
  3358. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3359. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3360. [item setValue:nsext forKey:@"customer_contact_ext"];
  3361. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3362. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3363. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3364. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3365. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3366. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3367. [item setValue:nslast_name forKey:@"customer_last_name"];
  3368. [item setValue:nscity forKey:@"customer_city"];
  3369. [item setValue:nsstate forKey:@"customer_state"];
  3370. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3371. [item setValue:contactId forKey:@"customer_cid"];
  3372. [item setValue:nscontact_name forKey:@"customer_contact"];
  3373. [item setValue:nsfax forKey:@"customer_fax"];
  3374. [item setValue:nsemail forKey:@"customer_email"];
  3375. [item setValue:contact_type forKey:@"customer_contact_type"];
  3376. [ret setObject:item forKey:@"customerInfo"];
  3377. // i++;
  3378. }
  3379. UIApplication * app = [UIApplication sharedApplication];
  3380. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3381. [ret setValue:appDelegate.mode forKey:@"mode"];
  3382. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3383. sqlite3_finalize(statement);
  3384. }
  3385. [iSalesDB close_db:db];
  3386. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3387. return ret;
  3388. }
  3389. + (bool) copy_bcardImg:(NSString*) filename
  3390. {
  3391. if(filename.length==0)
  3392. return false;
  3393. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3394. bool ret=false;
  3395. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3396. NSString *cachefolder = [paths objectAtIndex:0];
  3397. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3398. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3399. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3400. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3401. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3402. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3403. //
  3404. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3405. NSFileManager* fileManager = [NSFileManager defaultManager];
  3406. BOOL bdir=NO;
  3407. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3408. {
  3409. NSError *error = nil;
  3410. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3411. {
  3412. ret=false;
  3413. }
  3414. else
  3415. {
  3416. ret=true;
  3417. }
  3418. // NSError *error = nil;
  3419. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3420. //
  3421. // if(!bsuccess)
  3422. // {
  3423. // DebugLog(@"Create offline_createimg folder failed");
  3424. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3425. // return [RAUtils dict2data:ret];
  3426. // }
  3427. // if(bsuccess)
  3428. // {
  3429. // sqlite3 *db = [self get_db];
  3430. //
  3431. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3432. // [iSalesDB close_db:db];
  3433. // }
  3434. }
  3435. return ret;
  3436. //
  3437. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3438. // if(bsuccess)
  3439. // {
  3440. // NSError *error = nil;
  3441. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3442. // {
  3443. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3444. // }
  3445. // else
  3446. // {
  3447. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3448. //
  3449. // ret[@"img_url_aname"]=filename;
  3450. // ret[@"img_url"]=savedImagePath;
  3451. // }
  3452. // }
  3453. }
  3454. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3455. {
  3456. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3457. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3458. NSString *cachefolder = [paths objectAtIndex:0];
  3459. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3460. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3461. NSFileManager* fileManager = [NSFileManager defaultManager];
  3462. BOOL bdir=YES;
  3463. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3464. {
  3465. NSError *error = nil;
  3466. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3467. if(!bsuccess)
  3468. {
  3469. DebugLog(@"Create offline_createimg folder failed");
  3470. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3471. return [RAUtils dict2data:ret];
  3472. }
  3473. // if(bsuccess)
  3474. // {
  3475. // sqlite3 *db = [self get_db];
  3476. //
  3477. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3478. // [iSalesDB close_db:db];
  3479. // }
  3480. }
  3481. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3482. //JEPG格式
  3483. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3484. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3485. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3486. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3487. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3488. if(bsuccess)
  3489. {
  3490. NSError *error = nil;
  3491. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3492. {
  3493. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3494. }
  3495. else
  3496. {
  3497. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3498. ret[@"img_url_aname"]=filename;
  3499. ret[@"img_url"]=filename;
  3500. }
  3501. }
  3502. else
  3503. {
  3504. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3505. }
  3506. return [RAUtils dict2data:ret];
  3507. }
  3508. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3509. {
  3510. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3511. category = [category substringToIndex:3];
  3512. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3513. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3514. params[@"category"]= category;
  3515. ret[@"params"]= params;
  3516. [ret setValue:@"detail" forKey:@"target"];
  3517. [ret setValue:@"popup" forKey:@"action"];
  3518. [ret setValue:@"content" forKey:@"type"];
  3519. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3520. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3521. [ret setValue:@"true" forKey:@"single_row"];
  3522. [ret setValue:@"true" forKey:@"partial_refresh"];
  3523. // sqlite3 *db = [iSalesDB get_db];
  3524. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3525. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.product_id,i.url,random() as aa from product m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;",category,OFFLINE_IMG_TYPE_CATEGORY]; // select m.name,m.product_id,i.url,random() as aa from model m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;
  3526. sqlite3_stmt * statement;
  3527. int count = 0;
  3528. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3529. // int count=0;
  3530. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3531. {
  3532. int i=0;
  3533. while (sqlite3_step(statement) == SQLITE_ROW)
  3534. {
  3535. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3536. // char *name = (char*)sqlite3_column_text(statement, 1);
  3537. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3538. char *name = (char*)sqlite3_column_text(statement, 0);
  3539. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3540. int product_id = sqlite3_column_int(statement, 1);
  3541. char *url = (char*)sqlite3_column_text(statement, 2);
  3542. if(url==nil)
  3543. url="";
  3544. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3545. [item setValue:nsurl forKey:@"picture_path"];
  3546. [item setValue:nsname forKey:@"fash_name"];
  3547. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3548. [item setValue:category forKey:@"category"];
  3549. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3550. i++;
  3551. }
  3552. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3553. sqlite3_finalize(statement);
  3554. }
  3555. DebugLog(@"count:%d",count);
  3556. // [iSalesDB close_db:db];
  3557. return ret;
  3558. }
  3559. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3560. {
  3561. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3562. [ret setValue:key forKey:@"key"];
  3563. [ret setValue:value forKey:@"val"];
  3564. return ret;
  3565. }
  3566. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3567. {
  3568. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3569. [ret setValue:@"0" forKey:@"img_count"];
  3570. // sqlite3 *db = [iSalesDB get_db];
  3571. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,property_display from product where product_id=%d ;",field,product_id]; // select %@,property_display from model where product_id=%d ;
  3572. sqlite3_stmt * statement;
  3573. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3574. {
  3575. int i=0;
  3576. if (sqlite3_step(statement) == SQLITE_ROW)
  3577. {
  3578. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3579. // char *name = (char*)sqlite3_column_text(statement, 1);
  3580. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3581. char *value = (char*)sqlite3_column_text(statement, 0);
  3582. if(value==nil)
  3583. value="";
  3584. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3585. char *key = (char*)sqlite3_column_text(statement, 1);
  3586. if(key==nil)
  3587. key="";
  3588. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3589. [item setValue:nsvalue forKey:@"val"];
  3590. [item setValue:nskey forKey:@"key"];
  3591. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3592. [ret setValue:@"1" forKey:@"count"];
  3593. i++;
  3594. }
  3595. sqlite3_finalize(statement);
  3596. }
  3597. // [iSalesDB close_db:db];
  3598. return ret;
  3599. }
  3600. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3601. {
  3602. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3603. [ret setValue:@"0" forKey:@"count"];
  3604. // sqlite3 *db = [iSalesDB get_db];
  3605. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,selector_display,product_id,category from product where product_group='%@' order by name;",field,product_group]; // select %@,selector_display,product_id,category from model where product_group='%@' order by name;
  3606. sqlite3_stmt * statement;
  3607. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3608. {
  3609. int i=0;
  3610. while (sqlite3_step(statement) == SQLITE_ROW)
  3611. {
  3612. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3613. // char *name = (char*)sqlite3_column_text(statement, 1);
  3614. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3615. char *value = (char*)sqlite3_column_text(statement, 0);
  3616. if(value==nil)
  3617. value="";
  3618. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3619. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3620. if(selector_display==nil)
  3621. selector_display="";
  3622. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3623. int product_id = sqlite3_column_int(statement, 2);
  3624. char *category = (char*)sqlite3_column_text(statement, 3);
  3625. if(category==nil)
  3626. category="";
  3627. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3628. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3629. [item setValue:nsvalue forKey:@"title"];
  3630. [item setValue:url forKey:@"pic_url"];
  3631. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3632. [params setValue:@"2" forKey:@"count"];
  3633. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3634. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3635. [param0 setValue:@"product_id" forKey:@"name"];
  3636. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3637. [param1 setValue:nscategory forKey:@"val"];
  3638. [param1 setValue:@"category" forKey:@"name"];
  3639. [params setObject:param0 forKey:@"param_0"];
  3640. [params setObject:param1 forKey:@"param_1"];
  3641. [item setObject:params forKey:@"params"];
  3642. [ret setValue:nsselector_display forKey:@"name"];
  3643. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3644. i++;
  3645. }
  3646. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3647. [ret setValue:@"switch" forKey:@"action"];
  3648. sqlite3_finalize(statement);
  3649. }
  3650. // [iSalesDB close_db:db];
  3651. return ret;
  3652. }
  3653. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3654. {
  3655. // model 在 category search 显示的图片。
  3656. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3657. // sqlite3 *db = [iSalesDB get_db];
  3658. NSString *sqlQuery = [NSString stringWithFormat:@"select url,type from model_image where product_id=%d and type<2 order by default_img desc , _id asc limit 2;",product_id];
  3659. sqlite3_stmt * statement;
  3660. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3661. {
  3662. while (sqlite3_step(statement) == SQLITE_ROW)
  3663. {
  3664. char *url = (char*)sqlite3_column_text(statement, 0);
  3665. if(url==nil)
  3666. url="";
  3667. int type = sqlite3_column_int(statement, 1);
  3668. if(type==0)
  3669. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3670. else
  3671. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3672. }
  3673. sqlite3_finalize(statement);
  3674. }
  3675. // [iSalesDB close_db:db];
  3676. return ret;
  3677. }
  3678. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3679. {
  3680. int item_id=-1;
  3681. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3682. sqlite3_stmt * statement;
  3683. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3684. {
  3685. if (sqlite3_step(statement) == SQLITE_ROW)
  3686. {
  3687. item_id = sqlite3_column_int(statement, 0);
  3688. }
  3689. sqlite3_finalize(statement);
  3690. }
  3691. return item_id;
  3692. }
  3693. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3694. {
  3695. // NSString* ret = @"";
  3696. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3697. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3698. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3699. sqlite3_stmt * statement;
  3700. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3701. {
  3702. while (sqlite3_step(statement) == SQLITE_ROW)
  3703. {
  3704. int bitem_id = sqlite3_column_int(statement, 0);
  3705. int bitem_qty = sqlite3_column_int(statement, 1);
  3706. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3707. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3708. }
  3709. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3710. sqlite3_finalize(statement);
  3711. }
  3712. // if(ret==nil)
  3713. // ret=@"";
  3714. *count=arr_count;
  3715. return arr_bundle;
  3716. }
  3717. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3718. {
  3719. // get default sold qty, return -1 if model not found;
  3720. int ret = -1;
  3721. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3722. sqlite3_stmt * statement;
  3723. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3724. {
  3725. if (sqlite3_step(statement) == SQLITE_ROW)
  3726. {
  3727. ret = sqlite3_column_int(statement, 0);
  3728. }
  3729. sqlite3_finalize(statement);
  3730. }
  3731. return ret;
  3732. }
  3733. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3734. {
  3735. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3736. [ret setValue:@"0" forKey:@"img_count"];
  3737. // sqlite3 *db = [iSalesDB get_db];
  3738. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3739. sqlite3_stmt * statement;
  3740. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3741. {
  3742. int i=0;
  3743. while (sqlite3_step(statement) == SQLITE_ROW)
  3744. {
  3745. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3746. // char *name = (char*)sqlite3_column_text(statement, 1);
  3747. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3748. char *url = (char*)sqlite3_column_text(statement, 0);
  3749. if(url==nil)
  3750. url="";
  3751. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3752. [item setValue:nsurl forKey:@"s"];
  3753. [item setValue:nsurl forKey:@"l"];
  3754. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3755. i++;
  3756. }
  3757. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3758. sqlite3_finalize(statement);
  3759. }
  3760. // [iSalesDB close_db:db];
  3761. return ret;
  3762. }
  3763. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3764. UIApplication * app = [UIApplication sharedApplication];
  3765. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3766. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3767. sqlite3 *db = [iSalesDB get_db];
  3768. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3769. NSString* product_id=params[@"product_id"];
  3770. NSString *item_count_str = params[@"item_count"];
  3771. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3772. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3773. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3774. // NSString *sql = @"";
  3775. for(int i=0;i<arr.count;i++)
  3776. {
  3777. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3778. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3779. __block int cart_count = 0;
  3780. if (!item_count_str) {
  3781. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3782. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3783. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3784. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3785. cart_count = [[model_set_components lastObject] integerValue];
  3786. }];
  3787. }
  3788. if(count==0)
  3789. {
  3790. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3791. sqlite3_stmt *stmt;
  3792. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  3793. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  3794. if (item_count_arr) {
  3795. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  3796. } else {
  3797. sqlite3_bind_int(stmt,2,cart_count);
  3798. }
  3799. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3800. [iSalesDB execSql:@"ROLLBACK" db:db];
  3801. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3802. [iSalesDB close_db:db];
  3803. DebugLog(@"add to wishlist error");
  3804. return ret;
  3805. }
  3806. } else {
  3807. int qty = 0;
  3808. if (item_count_arr) {
  3809. qty = [item_count_arr[i] integerValue];
  3810. } else {
  3811. qty = cart_count;
  3812. }
  3813. sqlQuery = [NSString stringWithFormat:@"update wishlist set qty = 1 where product_id = %@ and (is_delete is null or is_delete != 1);update wishlist set qty = %d,is_delete = 0 where product_id = %@ and is_delete = 1;",arr[i],qty,arr[i]];
  3814. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  3815. [iSalesDB execSql:@"ROLLBACK" db:db];
  3816. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  3817. [iSalesDB close_db:db];
  3818. DebugLog(@"add to wishlist error");
  3819. return ret;
  3820. }
  3821. }
  3822. }
  3823. // [iSalesDB execSql:sql db:db];
  3824. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3825. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3826. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  3827. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  3828. [iSalesDB close_db:db];
  3829. appDelegate.wish_count =count;
  3830. [appDelegate update_count_mark];
  3831. ret[@"result"]= [NSNumber numberWithInt:2];
  3832. return ret;
  3833. }
  3834. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  3835. // 0 category
  3836. // 1 search
  3837. // 2 itemsearch
  3838. NSData *ret = nil;
  3839. NSDictionary *items = nil;
  3840. switch (from) {
  3841. case 0:{
  3842. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  3843. }
  3844. break;
  3845. case 1:{
  3846. items = [[self search:params limited:NO] objectForKey:@"items"];
  3847. }
  3848. break;
  3849. case 2:{
  3850. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  3851. }
  3852. break;
  3853. default:
  3854. break;
  3855. }
  3856. if (!items) {
  3857. return ret;
  3858. }
  3859. int count = [[items objectForKey:@"count"] intValue];
  3860. NSMutableString *product_id_str = [@"" mutableCopy];
  3861. for (int i = 0; i < count; i++) {
  3862. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  3863. NSDictionary *item = [items objectForKey:key];
  3864. NSString *product_id = [item objectForKey:@"product_id"];
  3865. if (i == 0) {
  3866. [product_id_str appendString:product_id];
  3867. } else {
  3868. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  3869. }
  3870. }
  3871. NSString *add_to = [self valueInParams:params key:@"addTo"];
  3872. if ([add_to isEqualToString:@"cart"]) {
  3873. NSString *order_code = [params objectForKey:@"orderCode"];
  3874. if (order_code.length) {
  3875. NSDictionary *newParams = @{
  3876. @"product_id" : product_id_str,
  3877. @"orderCode" : order_code
  3878. };
  3879. ret = [self offline_add2cart:newParams.mutableCopy];
  3880. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3881. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3882. ret = [RAUtils dict2data:retDic];
  3883. }
  3884. } else if([add_to isEqualToString:@"wishlist"]) {
  3885. NSDictionary *newParams = @{
  3886. @"product_id" : product_id_str
  3887. };
  3888. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  3889. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3890. ret = [RAUtils dict2data:retDic];
  3891. } else if([add_to isEqualToString:@"portfolio"]) {
  3892. }
  3893. return ret;
  3894. }
  3895. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  3896. {
  3897. return [self addAll:params from:0];
  3898. }
  3899. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  3900. {
  3901. return [self addAll:params from:1];
  3902. }
  3903. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  3904. {
  3905. return [self addAll:params from:2];
  3906. }
  3907. #pragma mark - Jack
  3908. #warning 做SQL操作时转义!!
  3909. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  3910. // "val_227" : {
  3911. // "check" : 1,
  3912. // "value" : "US United States",
  3913. // "value_id" : "228"
  3914. // },
  3915. if (!countryCode) {
  3916. countryCode = @"US";
  3917. }
  3918. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  3919. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  3920. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  3921. int code_id = sqlite3_column_int(stmt, 3); // id
  3922. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3923. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  3924. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  3925. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3926. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  3927. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3928. }
  3929. long n = *count;
  3930. *count = n + 1;
  3931. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  3932. [container setValue:countryDic forKey:key];
  3933. }] mutableCopy];
  3934. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3935. return ret;
  3936. }
  3937. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  3938. countryCode = [self translateSingleQuote:countryCode];
  3939. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  3940. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3941. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  3942. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  3943. if (name == NULL) {
  3944. name = "";
  3945. }
  3946. if (code == NULL) {
  3947. code = "";
  3948. }
  3949. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3950. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3951. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3952. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3953. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  3954. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3955. }
  3956. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3957. [container setValue:stateDic forKey:key];
  3958. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  3959. DebugLog(@"query all state error: %@",err_msg);
  3960. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3961. // [stateDic setValue:@"Other" forKey:@"value"];
  3962. // [stateDic setValue:@"" forKey:@"value_id"];
  3963. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3964. //
  3965. // if (state_code && [@"" isEqualToString:state_code]) {
  3966. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3967. // }
  3968. //
  3969. // NSString *key = [NSString stringWithFormat:@"val_0"];
  3970. // [container setValue:stateDic forKey:key];
  3971. }] mutableCopy];
  3972. [ret removeObjectForKey:@"result"];
  3973. // failure 可以不用了,一样的
  3974. if (ret.allKeys.count == 0) {
  3975. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3976. [stateDic setValue:@"Other" forKey:@"value"];
  3977. [stateDic setValue:@"" forKey:@"value_id"];
  3978. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3979. if (state_code && [@"" isEqualToString:state_code]) {
  3980. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3981. }
  3982. NSString *key = [NSString stringWithFormat:@"val_0"];
  3983. [ret setValue:stateDic forKey:key];
  3984. }
  3985. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3986. return ret;
  3987. }
  3988. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  3989. codeId = [self translateSingleQuote:codeId];
  3990. NSString *sql = [NSString stringWithFormat:@"select name,code from offline_state where country_code = (select code as country_code FROM offline_country where countrycode_id = %@);",codeId];
  3991. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3992. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  3993. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  3994. if (name == NULL) {
  3995. name = "";
  3996. }
  3997. if (code == NULL) {
  3998. code = "";
  3999. }
  4000. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4001. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4002. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4003. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4004. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4005. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4006. }
  4007. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4008. [container setValue:stateDic forKey:key];
  4009. }] mutableCopy];
  4010. [ret removeObjectForKey:@"result"];
  4011. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4012. return ret;
  4013. }
  4014. + (NSDictionary *)offline_getPrice {
  4015. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  4016. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4017. char *name = (char *) sqlite3_column_text(stmt, 1);
  4018. int type = sqlite3_column_int(stmt, 2);
  4019. int orderBy = sqlite3_column_int(stmt, 3);
  4020. if (name == NULL) {
  4021. name = "";
  4022. }
  4023. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4024. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4025. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4026. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  4027. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4028. if (orderBy == 0) {
  4029. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4030. }
  4031. [container setValue:priceDic forKey:key];
  4032. }] mutableCopy];
  4033. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4034. return ret;
  4035. }
  4036. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4037. NSString *sql = @"select _id,type_name from contact_type";
  4038. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4039. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4040. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4041. int _id = sqlite3_column_int(stmt, 0);
  4042. NSString *name = [self textAtColumn:1 statement:stmt];
  4043. NSDictionary *typeDic = @{
  4044. @"value_id" : name,
  4045. @"value" : name,
  4046. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4047. };
  4048. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4049. [ret setValue:[NSNumber numberWithInt:++(*count)] forKey:@"count"];
  4050. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4051. }];
  4052. return ret;
  4053. }
  4054. + (NSDictionary *)offline_getSalesRep {
  4055. // 首先从offline_login表中取出sales_code
  4056. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4057. NSString *user = app.user;
  4058. user = [self translateSingleQuote:user];
  4059. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4060. __block NSString *user_code = @"";
  4061. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4062. char *code = (char *)sqlite3_column_text(stmt, 0);
  4063. if (code == NULL) {
  4064. code = "";
  4065. }
  4066. user_code = [NSString stringWithUTF8String:code];
  4067. }];
  4068. // 再取所有salesRep
  4069. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4070. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4071. // 1 name 2 code 3 salesrep_id
  4072. char *name = (char *)sqlite3_column_text(stmt, 1);
  4073. char *code = (char *)sqlite3_column_text(stmt, 2);
  4074. int salesrep_id = sqlite3_column_int(stmt, 3);
  4075. if (name == NULL) {
  4076. name = "";
  4077. }
  4078. if (code == NULL) {
  4079. code = "";
  4080. }
  4081. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4082. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4083. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4084. // 比较code 相等则check
  4085. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4086. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4087. }
  4088. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4089. }] mutableCopy];
  4090. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4091. return ret;
  4092. }
  4093. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4094. zipcode = [self translateSingleQuote:zipcode];
  4095. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4096. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4097. char *country = (char *)sqlite3_column_text(stmt, 0);
  4098. char *state = (char *)sqlite3_column_text(stmt, 1);
  4099. char *city = (char *)sqlite3_column_text(stmt, 2);
  4100. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4101. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4102. if (country == NULL) {
  4103. country = "";
  4104. }
  4105. if (state == NULL) {
  4106. state = "";
  4107. }
  4108. if (city == NULL) {
  4109. city = "";
  4110. }
  4111. if (country_code == NULL) {
  4112. country_code = "";
  4113. }
  4114. if (state_code == NULL) {
  4115. state_code = "";
  4116. }
  4117. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4118. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4119. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4120. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4121. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4122. }] mutableCopy];
  4123. return ret;
  4124. }
  4125. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4126. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4127. [item setValue:value forKey:valueKey];
  4128. [dic setValue:item forKey:itemKey];
  4129. }
  4130. + (NSString *)countryCodeByid:(NSString *)code_id {
  4131. NSString *ret = nil;
  4132. code_id = [self translateSingleQuote:code_id];
  4133. sqlite3 *db = [iSalesDB get_db];
  4134. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4135. sqlite3_stmt * statement;
  4136. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4137. while (sqlite3_step(statement) == SQLITE_ROW) {
  4138. char *code = (char *)sqlite3_column_text(statement, 0);
  4139. if (code == NULL) {
  4140. code = "";
  4141. }
  4142. ret = [NSString stringWithUTF8String:code];
  4143. }
  4144. sqlite3_finalize(statement);
  4145. }
  4146. [iSalesDB close_db:db];
  4147. return ret;
  4148. }
  4149. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4150. NSString *ret = nil;
  4151. code = [self translateSingleQuote:code];
  4152. sqlite3 *db = [iSalesDB get_db];
  4153. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4154. sqlite3_stmt * statement;
  4155. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4156. while (sqlite3_step(statement) == SQLITE_ROW) {
  4157. char *_id = (char *)sqlite3_column_text(statement, 0);
  4158. if (_id == NULL) {
  4159. _id = "";
  4160. }
  4161. ret = [NSString stringWithFormat:@"%s",_id];
  4162. }
  4163. sqlite3_finalize(statement);
  4164. }
  4165. [iSalesDB close_db:db];
  4166. return ret;
  4167. }
  4168. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4169. NSString *name = nil;
  4170. codeId = [self translateSingleQuote:codeId];
  4171. sqlite3 *db = [iSalesDB get_db];
  4172. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4173. sqlite3_stmt * statement;
  4174. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4175. while (sqlite3_step(statement) == SQLITE_ROW) {
  4176. char *value = (char *)sqlite3_column_text(statement, 0);
  4177. if (value == NULL) {
  4178. value = "";
  4179. }
  4180. name = [NSString stringWithUTF8String:value];
  4181. }
  4182. sqlite3_finalize(statement);
  4183. }
  4184. [iSalesDB close_db:db];
  4185. return name;
  4186. }
  4187. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4188. NSString *ret = nil;
  4189. sqlite3 *db = [iSalesDB get_db];
  4190. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4191. sqlite3_stmt * statement;
  4192. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4193. while (sqlite3_step(statement) == SQLITE_ROW) {
  4194. char *name = (char *)sqlite3_column_text(statement, 0);
  4195. if (name == NULL) {
  4196. name = "";
  4197. }
  4198. ret = [NSString stringWithUTF8String:name];
  4199. }
  4200. sqlite3_finalize(statement);
  4201. }
  4202. [iSalesDB close_db:db];
  4203. return ret;
  4204. }
  4205. + (NSString *)salesRepCodeById:(NSString *)_id {
  4206. NSString *ret = nil;
  4207. _id = [self translateSingleQuote:_id];
  4208. sqlite3 *db = [iSalesDB get_db];
  4209. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4210. sqlite3_stmt * statement;
  4211. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4212. while (sqlite3_step(statement) == SQLITE_ROW) {
  4213. char *rep = (char *)sqlite3_column_text(statement, 0);
  4214. if (rep == NULL) {
  4215. rep = "";
  4216. }
  4217. ret = [NSString stringWithUTF8String:rep];
  4218. }
  4219. sqlite3_finalize(statement);
  4220. }
  4221. [iSalesDB close_db:db];
  4222. return ret;
  4223. }
  4224. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4225. char *tx = (char *)sqlite3_column_text(stmt, col);
  4226. if (tx == NULL) {
  4227. tx = "";
  4228. }
  4229. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4230. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4231. if (!text) {
  4232. text = @"";
  4233. }
  4234. // 将字符全部为' '的字符串干掉
  4235. int spaceCount = 0;
  4236. for (int i = 0; i < text.length; i++) {
  4237. if ([text characterAtIndex:i] == ' ') {
  4238. spaceCount++;
  4239. }
  4240. }
  4241. if (spaceCount == text.length) {
  4242. text = @"";
  4243. }
  4244. return text;
  4245. }
  4246. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4247. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4248. NSData *data = [NSData dataWithContentsOfFile:path];
  4249. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4250. return ret;
  4251. }
  4252. + (NSString *)textFileName:(NSString *)name {
  4253. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4254. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4255. if (!text) {
  4256. text = @"";
  4257. }
  4258. return text;
  4259. }
  4260. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4261. return [[dic objectForKey:key] mutableCopy];
  4262. }
  4263. + (id)translateSingleQuote:(NSString *)string {
  4264. if ([string isKindOfClass:[NSString class]])
  4265. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4266. return string;
  4267. }
  4268. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4269. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4270. }
  4271. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4272. NSString* ret= nil;
  4273. NSString *sqlQuery = nil;
  4274. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  4275. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ order by i.default_img desc, i._id asc limit 1;",item_id];
  4276. sqlite3_stmt * statement;
  4277. // int count=0;
  4278. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4279. {
  4280. if (sqlite3_step(statement) == SQLITE_ROW)
  4281. {
  4282. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4283. if(imgurl==nil)
  4284. imgurl="";
  4285. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4286. ret=nsimgurl;
  4287. }
  4288. sqlite3_finalize(statement);
  4289. }
  4290. else
  4291. {
  4292. [ret setValue:@"8" forKey:@"result"];
  4293. }
  4294. // [iSalesDB close_db:db];
  4295. // DebugLog(@"data string: %@",ret );
  4296. return ret;
  4297. }
  4298. #pragma mark contact list
  4299. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  4300. {
  4301. // contactType = "Sales_Order_Customer";
  4302. // limit = 25;
  4303. // offset = 0;
  4304. // password = 123456;
  4305. // "price_name" = 16;
  4306. // user = EvanK;
  4307. sqlite3 *db = [iSalesDB get_db];
  4308. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  4309. if (contactType) {
  4310. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  4311. } else {
  4312. contactType = @"1 = 1";
  4313. }
  4314. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  4315. DebugLog(@"offline contact list keyword: %@",keyword);
  4316. // advanced search
  4317. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  4318. if (contact_name) {
  4319. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4320. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  4321. } else {
  4322. contact_name = @"";
  4323. }
  4324. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  4325. if (customer_phone) {
  4326. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4327. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  4328. } else {
  4329. customer_phone = @"";
  4330. }
  4331. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  4332. if (customer_fax) {
  4333. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4334. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  4335. } else {
  4336. customer_fax = @"";
  4337. }
  4338. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  4339. if (customer_zipcode) {
  4340. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4341. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  4342. } else {
  4343. customer_zipcode = @"";
  4344. }
  4345. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  4346. if (customer_sales_rep) {
  4347. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4348. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  4349. } else {
  4350. customer_sales_rep = @"";
  4351. }
  4352. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  4353. if (customer_state) {
  4354. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4355. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  4356. } else {
  4357. customer_state = @"";
  4358. }
  4359. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  4360. if (customer_name) {
  4361. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4362. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  4363. } else {
  4364. customer_name = @"";
  4365. }
  4366. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  4367. if (customer_country) {
  4368. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4369. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  4370. } else {
  4371. customer_country = @"";
  4372. }
  4373. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  4374. if (customer_cid) {
  4375. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4376. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  4377. } else {
  4378. customer_cid = @"";
  4379. }
  4380. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  4381. if (customer_city) {
  4382. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4383. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  4384. } else {
  4385. customer_city = @"";
  4386. }
  4387. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  4388. if (customer_address) {
  4389. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4390. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  4391. } else {
  4392. customer_address = @"";
  4393. }
  4394. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  4395. if (customer_email) {
  4396. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4397. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  4398. } else {
  4399. customer_email = @"";
  4400. }
  4401. NSString *price_name = [params valueForKey:@"price_name"];
  4402. if (price_name) {
  4403. if ([price_name containsString:@","]) {
  4404. // 首先从 price表中查处name
  4405. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  4406. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  4407. for (int i = 1;i < pArray.count;i++) {
  4408. NSString *p = pArray[i];
  4409. [mutablePStr appendFormat:@" or type = %@ ",p];
  4410. }
  4411. [mutablePStr appendString:@";"];
  4412. __block NSMutableArray *price_name_array = [NSMutableArray array];
  4413. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4414. char *name = (char *)sqlite3_column_text(stmt, 0);
  4415. if (!name)
  4416. name = "";
  4417. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  4418. }];
  4419. // 再根据name 拼sql
  4420. NSMutableString *mutable_price_name = [NSMutableString string];
  4421. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  4422. for (int i = 1; i < price_name_array.count; i++) {
  4423. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  4424. }
  4425. [mutable_price_name appendString:@")"];
  4426. price_name = mutable_price_name;
  4427. } else {
  4428. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  4429. if ([price_name isEqualToString:@""]) {
  4430. price_name = @"";
  4431. } else {
  4432. __block NSString *price;
  4433. price_name = [self translateSingleQuote:price_name];
  4434. [iSalesDB jk_query:[NSString stringWithFormat:@"SELECT name FROM price where type = %@ ;",price_name] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4435. char *p = (char *)sqlite3_column_text(stmt, 0);
  4436. if (p == NULL) {
  4437. p = "";
  4438. }
  4439. price = [NSString stringWithUTF8String:p];
  4440. }];
  4441. if ([price isEqualToString:@""]) {
  4442. price_name = @"";
  4443. } else {
  4444. price = [self translateSingleQuote:price];
  4445. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  4446. }
  4447. }
  4448. }
  4449. } else {
  4450. price_name = @"";
  4451. }
  4452. int limit = [[params valueForKey:@"limit"] intValue];
  4453. int offset = [[params valueForKey:@"offset"] intValue];
  4454. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4455. NSString *where = [NSString stringWithFormat:@" %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ and is_active=1 ",contactType,contact_name,customer_phone,customer_fax,customer_zipcode,customer_sales_rep,customer_state,customer_name,customer_country,customer_cid,customer_city,customer_address,customer_email,price_name];
  4456. NSString *sql = [NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ order by decrypt(company_name) limit %d offset %d",where,limit,offset];
  4457. // int result= [iSalesDB AddExFunction:db];
  4458. int count =0;
  4459. NSString *sqlQuery = nil;
  4460. if(keyword.length==0)
  4461. {
  4462. // sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@=1 order by decrypt(company_name) limit %d offset %d",contactType , limit, offset];
  4463. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  4464. sqlQuery = sql;
  4465. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  4466. }
  4467. else
  4468. {
  4469. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4470. keyword = keyword.lowercaseString;
  4471. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4472. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ and (lower(contact_id ) like'%%%@%%' or lower(decrypt(company_name)) like '%%%@%%' or lower(decrypt(phone)) like '%%%@%%' or lower(decrypt(addr)) like '%%%@%%') and is_active=1 order by decrypt(company_name) limit %d offset %d",contactType , keyword,keyword,keyword,keyword,limit, offset];
  4473. count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@=1 and (lower(contact_id ) like'%@' or lower(company_name) like'%@') and is_active=1",contactType,keyword,keyword]];
  4474. }
  4475. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4476. sqlite3_stmt * statement;
  4477. [ret setValue:@"2" forKey:@"result"];
  4478. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4479. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4480. {
  4481. int i = 0;
  4482. while (sqlite3_step(statement) == SQLITE_ROW)
  4483. {
  4484. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4485. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4486. int editable = sqlite3_column_int(statement, 0);
  4487. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4488. NSString *nscompany_name =nil;
  4489. if(company_name==nil)
  4490. nscompany_name=@"";
  4491. else
  4492. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4493. char *country = (char*)sqlite3_column_text(statement, 2);
  4494. if(country==nil)
  4495. country="";
  4496. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4497. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4498. // if(addr==nil)
  4499. // addr="";
  4500. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4501. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4502. if(zipcode==nil)
  4503. zipcode="";
  4504. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4505. char *state = (char*)sqlite3_column_text(statement, 5);
  4506. if(state==nil)
  4507. state="";
  4508. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4509. char *city = (char*)sqlite3_column_text(statement, 6);
  4510. if(city==nil)
  4511. city="";
  4512. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4513. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4514. // NSString *nscontact_name = nil;
  4515. // if(contact_name==nil)
  4516. // nscontact_name=@"";
  4517. // else
  4518. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4519. char *phone = (char*)sqlite3_column_text(statement, 8);
  4520. NSString *nsphone = nil;
  4521. if(phone==nil)
  4522. nsphone=@"";
  4523. else
  4524. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4525. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4526. if(contact_id==nil)
  4527. contact_id="";
  4528. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4529. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4530. if(addr_1==nil)
  4531. addr_1="";
  4532. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4533. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4534. if(addr_2==nil)
  4535. addr_2="";
  4536. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4537. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4538. if(addr_3==nil)
  4539. addr_3="";
  4540. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4541. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4542. if(addr_4==nil)
  4543. addr_4="";
  4544. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4545. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4546. if(first_name==nil)
  4547. first_name="";
  4548. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4549. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4550. if(last_name==nil)
  4551. last_name="";
  4552. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4553. char *fax = (char*)sqlite3_column_text(statement, 16);
  4554. NSString *nsfax = nil;
  4555. if(fax==nil)
  4556. nsfax=@"";
  4557. else
  4558. {
  4559. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4560. if(nsfax.length>0)
  4561. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4562. }
  4563. char *email = (char*)sqlite3_column_text(statement, 17);
  4564. NSString *nsemail = nil;
  4565. if(email==nil)
  4566. nsemail=@"";
  4567. else
  4568. {
  4569. nsemail= [[NSString alloc]initWithUTF8String:email];
  4570. if(nsemail.length>0)
  4571. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4572. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4573. }
  4574. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4575. [arr_name addObject:nsfirst_name];
  4576. [arr_name addObject:nslast_name];
  4577. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  4578. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4579. {
  4580. // decrypt
  4581. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4582. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4583. nsphone=[AESCrypt fastdecrypt:nsphone];
  4584. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4585. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4586. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4587. }
  4588. [arr_addr addObject:nscompany_name];
  4589. [arr_addr addObject:nscontact_name];
  4590. [arr_addr addObject:@"<br>"];
  4591. [arr_addr addObject:nsaddr_1];
  4592. [arr_addr addObject:nsaddr_2];
  4593. [arr_addr addObject:nsaddr_3];
  4594. [arr_addr addObject:nsaddr_4];
  4595. //[arr_addr addObject:nsaddr];
  4596. [arr_addr addObject:nszipcode];
  4597. [arr_addr addObject:nscity];
  4598. [arr_addr addObject:nsstate];
  4599. [arr_addr addObject:nscountry];
  4600. [arr_addr addObject:@"<br>"];
  4601. [arr_addr addObject:nsphone];
  4602. [arr_addr addObject:nsfax];
  4603. [arr_addr addObject:nsemail];
  4604. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  4605. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4606. [item setValue:name forKey:@"name"];
  4607. [item setValue:nscontact_id forKey:@"contact_id"];
  4608. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4609. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4610. i++;
  4611. }
  4612. UIApplication * app = [UIApplication sharedApplication];
  4613. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4614. [ret setValue:appDelegate.mode forKey:@"mode"];
  4615. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4616. sqlite3_finalize(statement);
  4617. }
  4618. [iSalesDB close_db:db];
  4619. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4620. return ret;
  4621. }
  4622. #pragma mark contact Advanced search
  4623. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4624. {
  4625. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4626. return [RAUtils dict2data:contactAdvanceDic];
  4627. }
  4628. #pragma mark create new contact
  4629. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4630. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4631. NSData *data = [NSData dataWithContentsOfFile:path];
  4632. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4633. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4634. NSString *countryCode = nil;
  4635. NSString *countryCode_id = nil;
  4636. NSString *stateCode = nil;
  4637. NSString *city = nil;
  4638. NSString *zipCode = nil;
  4639. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4640. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4641. NSString *code_id = params[@"country"];
  4642. countryCode_id = code_id;
  4643. countryCode = [self countryCodeByid:code_id];
  4644. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4645. NSString *zip_code = params[@"zipcode"];
  4646. // 剔除全部为空格
  4647. int spaceCount = 0;
  4648. for (int i = 0; i < zip_code.length; i++) {
  4649. if ([zip_code characterAtIndex:i] == ' ') {
  4650. spaceCount++;
  4651. }
  4652. }
  4653. if (spaceCount == zip_code.length) {
  4654. zip_code = @"";
  4655. }
  4656. zipCode = zip_code;
  4657. if (zipCode.length > 0) {
  4658. countryCode_id = params[@"country"];
  4659. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4660. countryCode = [dic valueForKey:@"country_code"];
  4661. if (!countryCode) {
  4662. // countryCode = @"US";
  4663. NSString *code_id = params[@"country"];
  4664. countryCode = [self countryCodeByid:code_id];
  4665. }
  4666. stateCode = [dic valueForKey:@"state_code"];
  4667. if (!stateCode.length) {
  4668. stateCode = params[@"state"];
  4669. }
  4670. city = [dic valueForKey:@"city"];
  4671. if (!city.length) {
  4672. city = params[@"city"];
  4673. }
  4674. // zip code
  4675. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4676. [zipDic setValue:zipCode forKey:@"value"];
  4677. [section_0 setValue:zipDic forKey:@"item_11"];
  4678. } else {
  4679. NSString *code_id = params[@"country"];
  4680. countryCode = [self countryCodeByid:code_id];
  4681. stateCode = params[@"state"];
  4682. city = params[@"city"];
  4683. }
  4684. }
  4685. } else {
  4686. // default: US United States
  4687. countryCode = @"US";
  4688. countryCode_id = @"228";
  4689. }
  4690. // country
  4691. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4692. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4693. // state
  4694. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4695. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4696. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4697. // city
  4698. if (city) {
  4699. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4700. [cityDic setValue:city forKey:@"value"];
  4701. [section_0 setValue:cityDic forKey:@"item_13"];
  4702. }
  4703. // price type
  4704. NSDictionary *priceDic = [self offline_getPrice];
  4705. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4706. // contact type
  4707. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4708. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4709. // Sales Rep
  4710. NSDictionary *repDic = [self offline_getSalesRep];
  4711. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4712. [ret setValue:section_0 forKey:@"section_0"];
  4713. return [RAUtils dict2data:ret];
  4714. }
  4715. #pragma mark save
  4716. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4717. NSString *addr = nil;
  4718. NSString *contact_name = nil;
  4719. NSString *companyName = [params objectForKey:@"company"];
  4720. if (companyName) {
  4721. companyName = [AESCrypt fastencrypt:companyName];
  4722. } else {
  4723. companyName = @"";
  4724. }
  4725. DebugLog(@"company");
  4726. companyName = [self translateSingleQuote:companyName];
  4727. NSString *addr1 = [params objectForKey:@"address"];
  4728. NSString *addr2 = [params objectForKey:@"address2"];
  4729. NSString *addr3 = [params objectForKey:@"address_3"];
  4730. NSString *addr4 = [params objectForKey:@"address_4"];
  4731. if (!addr2) {
  4732. addr2 = @"";
  4733. }
  4734. if (!addr3) {
  4735. addr3 = @"";
  4736. }
  4737. if (!addr4) {
  4738. addr4 = @"";
  4739. }
  4740. if (!addr1) {
  4741. addr1 = @"";
  4742. }
  4743. DebugLog(@"addr");
  4744. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4745. addr = [AESCrypt fastencrypt:addr];
  4746. addr = [self translateSingleQuote:addr];
  4747. if (addr1 && ![addr1 isEqualToString:@""]) {
  4748. addr1 = [AESCrypt fastencrypt:addr1];
  4749. }
  4750. addr1 = [self translateSingleQuote:addr1];
  4751. addr2 = [self translateSingleQuote:addr2];
  4752. addr3 = [self translateSingleQuote:addr3];
  4753. addr4 = [self translateSingleQuote:addr4];
  4754. NSString *country = [params objectForKey:@"country"];
  4755. if (country) {
  4756. country = [self countryNameByCountryCodeId:country];
  4757. } else {
  4758. country = @"";
  4759. }
  4760. DebugLog(@"country");
  4761. country = [self translateSingleQuote:country];
  4762. NSString *state = [params objectForKey:@"state"];
  4763. if (!state) {
  4764. state = @"";
  4765. }
  4766. DebugLog(@"state");
  4767. state = [self translateSingleQuote:state];
  4768. NSString *city = [params objectForKey:@"city"];
  4769. if (!city) {
  4770. city = @"";
  4771. }
  4772. city = [self translateSingleQuote:city];
  4773. NSString *zipcode = [params objectForKey:@"zipcode"];
  4774. if (!zipcode) {
  4775. zipcode = @"";
  4776. }
  4777. DebugLog(@"zip");
  4778. zipcode = [self translateSingleQuote:zipcode];
  4779. NSString *fistName = [params objectForKey:@"firstname"];
  4780. if (!fistName) {
  4781. fistName = @"";
  4782. }
  4783. NSString *lastName = [params objectForKey:@"lastname"];
  4784. if (!lastName) {
  4785. lastName = @"";
  4786. }
  4787. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  4788. DebugLog(@"contact_name");
  4789. contact_name = [self translateSingleQuote:contact_name];
  4790. fistName = [self translateSingleQuote:fistName];
  4791. lastName = [self translateSingleQuote:lastName];
  4792. NSString *phone = [params objectForKey:@"phone"];
  4793. if (phone) {
  4794. phone = [AESCrypt fastencrypt:phone];
  4795. } else {
  4796. phone = @"";
  4797. }
  4798. DebugLog(@"PHONE");
  4799. phone = [self translateSingleQuote:phone];
  4800. NSString *fax = [params objectForKey:@"fax"];
  4801. if (!fax) {
  4802. fax = @"";
  4803. }
  4804. DebugLog(@"FAX");
  4805. fax = [self translateSingleQuote:fax];
  4806. NSString *email = [params objectForKey:@"email"];
  4807. if (!email) {
  4808. email = @"";
  4809. }
  4810. DebugLog(@"EMAIL:%@",email);
  4811. email = [self translateSingleQuote:email];
  4812. NSString *notes = [params objectForKey:@"contact_notes"];
  4813. if (!notes) {
  4814. notes = @"";
  4815. }
  4816. DebugLog(@"NOTE:%@",notes);
  4817. notes = [self translateSingleQuote:notes];
  4818. NSString *price = [params objectForKey:@"price_name"];
  4819. if (price) {
  4820. price = [self priceNameByPriceId:price];
  4821. } else {
  4822. price = @"";
  4823. }
  4824. DebugLog(@"PRICE");
  4825. price = [self translateSingleQuote:price];
  4826. NSString *salesRep = [params objectForKey:@"sales_rep"];
  4827. if (salesRep) {
  4828. salesRep = [self salesRepCodeById:salesRep];
  4829. } else {
  4830. salesRep = @"";
  4831. }
  4832. salesRep = [self translateSingleQuote:salesRep];
  4833. NSString *img = [params objectForKey:@"business_card"];
  4834. NSArray *array = [img componentsSeparatedByString:@","];
  4835. NSString *img_0 = array[0];
  4836. if (!img_0) {
  4837. img_0 = @"";
  4838. }
  4839. img_0 = [self translateSingleQuote:img_0];
  4840. NSString *img_1 = array[1];
  4841. if (!img_1) {
  4842. img_1 = @"";
  4843. }
  4844. img_1 = [self translateSingleQuote:img_1];
  4845. NSString *img_2 = array[2];
  4846. if (!img_2) {
  4847. img_2 = @"";
  4848. }
  4849. img_2 = [self translateSingleQuote:img_2];
  4850. NSString *contact_id = [NSUUID UUID].UUIDString;
  4851. NSString *contact_type = [params objectForKey:@"type_name"];
  4852. if (!contact_type) {
  4853. contact_type = @"";
  4854. }
  4855. contact_type = [self translateSingleQuote:contact_type];
  4856. // 判断更新时是否为customer
  4857. if (update) {
  4858. contact_id = [params objectForKey:@"contact_id"];
  4859. if (!contact_id) {
  4860. contact_id = @"";
  4861. }
  4862. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  4863. __block int customer = 0;
  4864. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4865. customer = sqlite3_column_int(stmt, 0);
  4866. }];
  4867. isCustomer = customer ? YES : NO;
  4868. }
  4869. NSMutableDictionary *sync_dic = [params mutableCopy];
  4870. if (isCustomer) {
  4871. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  4872. } else {
  4873. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  4874. }
  4875. NSString *sync_data = nil;
  4876. NSString *sql = nil;
  4877. if (update){
  4878. contact_id = [params objectForKey:@"contact_id"];
  4879. if (!contact_id) {
  4880. contact_id = @"";
  4881. }
  4882. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4883. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4884. sync_data = [RAUtils dict2string:sync_dic];
  4885. sync_data = [self translateSingleQuote:sync_data];
  4886. sql = [NSString stringWithFormat:@"update offline_contact set company_name = '%@',addr_1 = '%@',addr_2 = '%@',addr_3 = '%@',addr_4 = '%@',country = '%@',state = '%@',city = '%@',zipcode = '%@',first_name = '%@',last_name = '%@',phone = '%@',fax = '%@',email = '%@',notes = '%@',price_type = '%@',sales_rep = '%@',img_0 = '%@',img_1 = '%@',img_2 = '%@',sync_data = '%@',contact_name = '%@',addr = '%@',type = '%@' where contact_id = '%@';",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,sync_data,contact_name,addr,contact_type,contact_id];
  4887. } else {
  4888. contact_id = [self translateSingleQuote:contact_id];
  4889. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4890. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4891. sync_data = [RAUtils dict2string:sync_dic];
  4892. sync_data = [self translateSingleQuote:sync_data];
  4893. sql = [NSString stringWithFormat:@"insert into offline_contact (is_active,company_name,addr_1,addr_2,addr_3,addr_4,country,state,city,zipcode,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,editable,contact_id,Sales_Order_Customer,Sales_Order_Freight_Bill_To,Sales_Order_Ship_From,Sales_Order_Merchandise_Bill_To,Contact_Return_To,Sales_Order_Ship_To,sync_data,contact_name,addr,type) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',%d,%d,0,%d,0,1,'%@','%@','%@','%@')",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id,isCustomer ? 1 : 0,isCustomer ? 1 : 0,isCustomer ? 1 : 0,sync_data,contact_name,addr,contact_type];
  4894. }
  4895. int result = [iSalesDB execSql:sql];
  4896. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  4897. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  4898. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  4899. }
  4900. #pragma mark save new contact
  4901. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  4902. {
  4903. return [self offline_saveContact:params update:NO isCustomer:YES];
  4904. }
  4905. #pragma mark edit contact
  4906. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  4907. {
  4908. // {
  4909. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  4910. // password = 123456;
  4911. // user = EvanK;
  4912. // }
  4913. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  4914. NSData *data = [NSData dataWithContentsOfFile:path];
  4915. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4916. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4917. NSString *countryCode = nil;
  4918. NSString *countryCode_id = nil;
  4919. NSString *stateCode = nil;
  4920. /*------contact infor------*/
  4921. __block NSString *country = nil;
  4922. __block NSString *company_name = nil;
  4923. __block NSString *contact_id = params[@"contact_id"];
  4924. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  4925. __block NSString *zipcode = nil;
  4926. __block NSString *state = nil; // state_code
  4927. __block NSString *city = nil; //
  4928. __block NSString *firt_name,*last_name;
  4929. __block NSString *phone,*fax,*email;
  4930. __block NSString *notes,*price_type,*sales_rep;
  4931. __block NSString *img_0,*img_1,*img_2;
  4932. __block NSString *contact_type;
  4933. contact_id = [self translateSingleQuote:contact_id];
  4934. NSString *sql = [NSString stringWithFormat:@"select country,company_name,addr_1,addr_2,addr_3,addr_4,zipcode,state,city,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,type from offline_contact where contact_id = '%@';",contact_id];
  4935. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4936. country = [self textAtColumn:0 statement:stmt]; // country name
  4937. company_name = [self textAtColumn:1 statement:stmt];
  4938. addr_1 = [self textAtColumn:2 statement:stmt];
  4939. addr_2 = [self textAtColumn:3 statement:stmt];
  4940. addr_3 = [self textAtColumn:4 statement:stmt];
  4941. addr_4 = [self textAtColumn:5 statement:stmt];
  4942. zipcode = [self textAtColumn:6 statement:stmt];
  4943. state = [self textAtColumn:7 statement:stmt]; // state code
  4944. city = [self textAtColumn:8 statement:stmt];
  4945. firt_name = [self textAtColumn:9 statement:stmt];
  4946. last_name = [self textAtColumn:10 statement:stmt];
  4947. phone = [self textAtColumn:11 statement:stmt];
  4948. fax = [self textAtColumn:12 statement:stmt];
  4949. email = [self textAtColumn:13 statement:stmt];
  4950. notes = [self textAtColumn:14 statement:stmt];
  4951. price_type = [self textAtColumn:15 statement:stmt]; // name
  4952. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  4953. img_0 = [self textAtColumn:17 statement:stmt];
  4954. img_1 = [self textAtColumn:18 statement:stmt];
  4955. img_2 = [self textAtColumn:19 statement:stmt];
  4956. contact_type = [self textAtColumn:20 statement:stmt];
  4957. }];
  4958. // decrypt
  4959. if (company_name) {
  4960. company_name = [AESCrypt fastdecrypt:company_name];
  4961. }
  4962. if (addr_1) {
  4963. addr_1 = [AESCrypt fastdecrypt:addr_1];
  4964. }
  4965. if (phone) {
  4966. phone = [AESCrypt fastdecrypt:phone];
  4967. }
  4968. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  4969. countryCode = [iSalesDB jk_queryText:countrySql];
  4970. stateCode = state;
  4971. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4972. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  4973. NSString *code_id = params[@"country"];
  4974. countryCode_id = code_id;
  4975. countryCode = [self countryCodeByid:code_id];
  4976. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  4977. NSString *zip_code = params[@"zipcode"];
  4978. // 剔除全部为空格
  4979. int spaceCount = 0;
  4980. for (int i = 0; i < zip_code.length; i++) {
  4981. if ([zip_code characterAtIndex:i] == ' ') {
  4982. spaceCount++;
  4983. }
  4984. }
  4985. if (spaceCount == zip_code.length) {
  4986. zip_code = @"";
  4987. }
  4988. if (zipcode.length > 0) {
  4989. zipcode = zip_code;
  4990. countryCode_id = params[@"country"];
  4991. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4992. countryCode = [dic valueForKey:@"country_code"];
  4993. if (!countryCode) {
  4994. NSString *code_id = params[@"country"];
  4995. countryCode = [self countryCodeByid:code_id];
  4996. }
  4997. stateCode = [dic valueForKey:@"state_code"];
  4998. if (!stateCode.length) {
  4999. stateCode = params[@"state"];
  5000. }
  5001. city = [dic valueForKey:@"city"];
  5002. if (!city.length) {
  5003. city = params[@"city"];
  5004. }
  5005. // zip code
  5006. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5007. [zipDic setValue:zipcode forKey:@"value"];
  5008. [section_0 setValue:zipDic forKey:@"item_8"];
  5009. } else {
  5010. NSString *code_id = params[@"country"];
  5011. countryCode = [self countryCodeByid:code_id];
  5012. stateCode = params[@"state"];
  5013. city = params[@"city"];
  5014. }
  5015. }
  5016. }
  5017. // 0 Country
  5018. // 1 Company Name
  5019. // 2 Contact ID
  5020. // 3 Picture
  5021. // 4 Address 1
  5022. // 5 Address 2
  5023. // 6 Address 3
  5024. // 7 Address 4
  5025. // 8 Zip Code
  5026. // 9 State/Province
  5027. // 10 City
  5028. // 11 Contact First Name
  5029. // 12 Contact Last Name
  5030. // 13 Phone
  5031. // 14 Fax
  5032. // 15 Email
  5033. // 16 Contact Notes
  5034. // 17 Price Type
  5035. // 18 Contact Type
  5036. // 19 Sales Rep
  5037. // country
  5038. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5039. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5040. // company
  5041. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5042. // contact_id
  5043. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5044. // picture
  5045. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5046. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5047. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5048. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5049. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5050. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5051. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5052. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5053. // addr 1 2 3 4
  5054. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5055. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5056. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5057. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5058. // zip code
  5059. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5060. // state
  5061. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5062. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5063. // city
  5064. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5065. // first last
  5066. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5067. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5068. // phone fax email
  5069. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5070. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5071. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5072. // notes
  5073. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5074. // price
  5075. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5076. for (NSString *key in priceDic.allKeys) {
  5077. if ([key containsString:@"val_"]) {
  5078. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5079. if ([dic[@"value"] isEqualToString:price_type]) {
  5080. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5081. [priceDic setValue:dic forKey:key];
  5082. }
  5083. }
  5084. }
  5085. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5086. // Contact Rep
  5087. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5088. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5089. // Sales Rep
  5090. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  5091. for (NSString *key in repDic.allKeys) {
  5092. if ([key containsString:@"val_"]) {
  5093. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5094. NSString *value = dic[@"value"];
  5095. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5096. if (code && [code isEqualToString:sales_rep]) {
  5097. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5098. [repDic setValue:dic forKey:key];
  5099. }
  5100. }
  5101. }
  5102. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5103. [ret setValue:section_0 forKey:@"section_0"];
  5104. return [RAUtils dict2data:ret];
  5105. }
  5106. #pragma mark save contact
  5107. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5108. {
  5109. return [self offline_saveContact:params update:YES isCustomer:YES];
  5110. }
  5111. #pragma mark category
  5112. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5113. if (ck) {
  5114. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5115. for (NSString *key in res.allKeys) {
  5116. if (![key isEqualToString:@"count"]) {
  5117. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5118. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5119. if ([val[@"value"] isEqualToString:ck]) {
  5120. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5121. }
  5122. [res setValue:val forKey:key];
  5123. }
  5124. }
  5125. [dic setValue:res forKey:valueKey];
  5126. }
  5127. }
  5128. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5129. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5130. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5131. NSString* category = [params valueForKey:@"category"];
  5132. if (!category || [category isEqualToString:@""]) {
  5133. category = @"%";
  5134. }
  5135. category = [self translateSingleQuote:category];
  5136. int limit = [[params valueForKey:@"limit"] intValue];
  5137. int offset = [[params valueForKey:@"offset"] intValue];
  5138. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5139. NSString *limit_str = @"";
  5140. if (limited) {
  5141. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5142. }
  5143. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5144. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5145. sqlite3 *db = [iSalesDB get_db];
  5146. // [iSalesDB AddExFunction:db];
  5147. int count;
  5148. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5149. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where m.category like'%%#%@#%%' and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, category,limit_str];
  5150. double price_min = 0;
  5151. double price_max = 0;
  5152. if ([params.allKeys containsObject:@"alert"]) {
  5153. // alert
  5154. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5155. NSString *alert = params[@"alert"];
  5156. if ([alert isEqualToString:@"Display All"]) {
  5157. alert = [NSString stringWithFormat:@""];
  5158. } else {
  5159. alert = [self translateSingleQuote:alert];
  5160. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5161. }
  5162. // available
  5163. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5164. NSString *available = params[@"available"];
  5165. NSString *available_condition;
  5166. if ([available isEqualToString:@"Display All"]) {
  5167. available_condition = @"";
  5168. } else if ([available isEqualToString:@"Available Now"]) {
  5169. available_condition = @"and availability > 0";
  5170. } else {
  5171. available_condition = @"and availability == 0";
  5172. }
  5173. // best seller
  5174. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5175. NSString *best_seller = @"";
  5176. NSString *order_best_seller = @"m.name asc";
  5177. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5178. best_seller = @"and best_seller > 0";
  5179. order_best_seller = @"m.best_seller desc,m.name asc";
  5180. }
  5181. // price
  5182. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5183. NSString *price = params[@"price"];
  5184. price_min = 0;
  5185. price_max = MAXFLOAT;
  5186. if (appDelegate.user) {
  5187. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5188. NSMutableString *priceName = [NSMutableString string];
  5189. for (int i = 0; i < priceTypeArray.count; i++) {
  5190. NSString *pricetype = priceTypeArray[i];
  5191. pricetype = [self translateSingleQuote:pricetype];
  5192. if (i == 0) {
  5193. [priceName appendFormat:@"'%@'",pricetype];
  5194. } else {
  5195. [priceName appendFormat:@",'%@'",pricetype];
  5196. }
  5197. }
  5198. if ([price isEqualToString:@"Display All"]) {
  5199. price = [NSString stringWithFormat:@""];
  5200. } else if([price containsString:@"+"]){
  5201. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5202. price_min = [price doubleValue];
  5203. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  5204. } else {
  5205. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5206. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5207. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5208. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  5209. }
  5210. } else {
  5211. price = @"";
  5212. }
  5213. // sold_by_qty : Sold in quantities of %@
  5214. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5215. NSString *qty = params[@"sold_by_qty"];
  5216. if ([qty isEqualToString:@"Display All"]) {
  5217. qty = @"";
  5218. } else {
  5219. qty = [self translateSingleQuote:qty];
  5220. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5221. }
  5222. // cate
  5223. category = [self translateSingleQuote:category];
  5224. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5225. // where bestseller > 0 order by bestseller desc
  5226. // sql query: alert availability(int) best_seller(int) price qty
  5227. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from (select name,description,product_id,closeout,best_seller from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;",cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  5228. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5229. }
  5230. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5231. if (!appDelegate.user) {
  5232. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5233. }
  5234. [ret setValue:filter forKey:@"filter"];
  5235. DebugLog(@"offline_category sql:%@",sqlQuery);
  5236. sqlite3_stmt * statement;
  5237. [ret setValue:@"2" forKey:@"result"];
  5238. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5239. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5240. // int count=0;
  5241. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5242. {
  5243. int i=0;
  5244. while (sqlite3_step(statement) == SQLITE_ROW)
  5245. {
  5246. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5247. char *name = (char*)sqlite3_column_text(statement, 0);
  5248. if(name==nil)
  5249. name="";
  5250. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5251. char *description = (char*)sqlite3_column_text(statement, 1);
  5252. if(description==nil)
  5253. description="";
  5254. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5255. int product_id = sqlite3_column_int(statement, 2);
  5256. int wid = sqlite3_column_int(statement, 3);
  5257. int closeout = sqlite3_column_int(statement, 4);
  5258. int cid = sqlite3_column_int(statement, 5);
  5259. int wisdelete = sqlite3_column_int(statement, 6);
  5260. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5261. if(wid !=0 && wisdelete != 1)
  5262. [item setValue:@"true" forKey:@"wish_exists"];
  5263. else
  5264. [item setValue:@"false" forKey:@"wish_exists"];
  5265. if(closeout==0)
  5266. [item setValue:@"false" forKey:@"is_closeout"];
  5267. else
  5268. [item setValue:@"true" forKey:@"is_closeout"];
  5269. if(cid==0)
  5270. [item setValue:@"false" forKey:@"cart_exists"];
  5271. else
  5272. [item setValue:@"true" forKey:@"cart_exists"];
  5273. [item addEntriesFromDictionary:imgjson];
  5274. // [item setValue:nsurl forKey:@"img"];
  5275. [item setValue:nsname forKey:@"name"];
  5276. [item setValue:nsdescription forKey:@"description"];
  5277. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5278. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5279. i++;
  5280. }
  5281. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5282. [ret setObject:items forKey:@"items"];
  5283. sqlite3_finalize(statement);
  5284. } else {
  5285. DebugLog(@"nothing...");
  5286. }
  5287. DebugLog(@"count:%d",count);
  5288. [iSalesDB close_db:db];
  5289. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5290. return ret;
  5291. }
  5292. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5293. {
  5294. return [self categoryList:params limited:YES];
  5295. }
  5296. # pragma mark item search
  5297. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5298. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5299. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5300. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5301. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5302. // category
  5303. NSDictionary *category_menu = [self offline_category_menu];
  5304. [filter setValue:category_menu forKey:@"category"];
  5305. NSString* where= nil;
  5306. NSString* orderby= @"m.name";
  5307. if (!filterSearch) {
  5308. int covertype = [[params valueForKey:@"covertype"] intValue];
  5309. switch (covertype) {
  5310. case 0:
  5311. {
  5312. where=@"m.category like'%%#005#%%'";
  5313. break;
  5314. }
  5315. case 1:
  5316. {
  5317. where=@"m.alert like '%QS%'";
  5318. break;
  5319. }
  5320. case 2:
  5321. {
  5322. where=@"m.availability>0";
  5323. break;
  5324. }
  5325. case 3:
  5326. {
  5327. where=@"m.best_seller>0";
  5328. orderby=@"m.best_seller desc,m.name asc";
  5329. break;
  5330. }
  5331. default:
  5332. where=@"1=1";
  5333. break;
  5334. }
  5335. }
  5336. int limit = [[params valueForKey:@"limit"] intValue];
  5337. int offset = [[params valueForKey:@"offset"] intValue];
  5338. NSString *limit_str = @"";
  5339. if (limited) {
  5340. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5341. }
  5342. sqlite3 *db = [iSalesDB get_db];
  5343. // [iSalesDB AddExFunction:db];
  5344. int count;
  5345. NSString *sqlQuery = nil;
  5346. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5347. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where %@ order by %@ %@;",appDelegate.order_code, where,orderby,limit_str];
  5348. double price_min = 0;
  5349. double price_max = 0;
  5350. if (filterSearch) {
  5351. // alert
  5352. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5353. NSString *alert = params[@"alert"];
  5354. if ([alert isEqualToString:@"Display All"]) {
  5355. alert = [NSString stringWithFormat:@""];
  5356. } else {
  5357. alert = [self translateSingleQuote:alert];
  5358. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5359. }
  5360. // available
  5361. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5362. NSString *available = params[@"available"];
  5363. NSString *available_condition;
  5364. if ([available isEqualToString:@"Display All"]) {
  5365. available_condition = @"";
  5366. } else if ([available isEqualToString:@"Available Now"]) {
  5367. available_condition = @"and availability > 0";
  5368. } else {
  5369. available_condition = @"and availability == 0";
  5370. }
  5371. // best seller
  5372. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5373. NSString *best_seller = @"";
  5374. NSString *order_best_seller = @"m.name asc";
  5375. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5376. best_seller = @"and best_seller > 0";
  5377. order_best_seller = @"m.best_seller desc,m.name asc";
  5378. }
  5379. // price
  5380. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5381. NSString *price = params[@"price"];
  5382. price_min = 0;
  5383. price_max = MAXFLOAT;
  5384. if (appDelegate.user) {
  5385. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5386. NSMutableString *priceName = [NSMutableString string];
  5387. for (int i = 0; i < priceTypeArray.count; i++) {
  5388. NSString *pricetype = priceTypeArray[i];
  5389. pricetype = [self translateSingleQuote:pricetype];
  5390. if (i == 0) {
  5391. [priceName appendFormat:@"'%@'",pricetype];
  5392. } else {
  5393. [priceName appendFormat:@",'%@'",pricetype];
  5394. }
  5395. }
  5396. if ([price isEqualToString:@"Display All"]) {
  5397. price = [NSString stringWithFormat:@""];
  5398. } else if([price containsString:@"+"]){
  5399. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5400. price_min = [price doubleValue];
  5401. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  5402. } else {
  5403. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5404. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5405. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5406. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  5407. }
  5408. } else {
  5409. price = @"";
  5410. }
  5411. // sold_by_qty : Sold in quantities of %@
  5412. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5413. NSString *qty = params[@"sold_by_qty"];
  5414. if ([qty isEqualToString:@"Display All"]) {
  5415. qty = @"";
  5416. } else {
  5417. qty = [self translateSingleQuote:qty];
  5418. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5419. }
  5420. // category
  5421. NSString *category_id = params[@"ctgId"];
  5422. NSMutableArray *cate_id_array = nil;
  5423. NSMutableString *cateWhere = [NSMutableString string];
  5424. if ([category_id isEqualToString:@""] || !category_id) {
  5425. [cateWhere appendString:@"1 = 1"];
  5426. } else {
  5427. if ([category_id containsString:@","]) {
  5428. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5429. } else {
  5430. cate_id_array = [@[category_id] mutableCopy];
  5431. }
  5432. /*-----------*/
  5433. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  5434. for (int i = 0; i < cate_id_array.count; i++) {
  5435. for (NSString *key0 in cateDic.allKeys) {
  5436. if ([key0 containsString:@"category_"]) {
  5437. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  5438. for (NSString *key1 in category0.allKeys) {
  5439. if ([key1 containsString:@"category_"]) {
  5440. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  5441. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5442. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5443. cate_id_array[i] = [category1 objectForKey:@"id"];
  5444. if (i == 0) {
  5445. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5446. } else {
  5447. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  5448. }
  5449. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5450. [category0 setValue:category1 forKey:key1];
  5451. [cateDic setValue:category0 forKey:key0];
  5452. }
  5453. }
  5454. }
  5455. }
  5456. }
  5457. }
  5458. [filter setValue:cateDic forKey:@"category"];
  5459. }
  5460. // where bestseller > 0 order by bestseller desc
  5461. // sql query: alert availability(int) best_seller(int) price qty
  5462. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from (select name,description,product_id,closeout,best_seller from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;", cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  5463. // count
  5464. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5465. }
  5466. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5467. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5468. if (!appDelegate.user) {
  5469. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5470. }
  5471. [ret setValue:filter forKey:@"filter"];
  5472. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5473. sqlite3_stmt * statement;
  5474. [ret setValue:@"2" forKey:@"result"];
  5475. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5476. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5477. // int count=0;
  5478. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5479. {
  5480. int i=0;
  5481. while (sqlite3_step(statement) == SQLITE_ROW)
  5482. {
  5483. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5484. char *name = (char*)sqlite3_column_text(statement, 0);
  5485. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5486. char *description = (char*)sqlite3_column_text(statement, 1);
  5487. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5488. int product_id = sqlite3_column_int(statement, 2);
  5489. int wid = sqlite3_column_int(statement, 3);
  5490. int closeout = sqlite3_column_int(statement, 4);
  5491. int cid = sqlite3_column_int(statement, 5);
  5492. int wisdelete = sqlite3_column_int(statement, 6);
  5493. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5494. if(wid !=0 && wisdelete != 1)
  5495. [item setValue:@"true" forKey:@"wish_exists"];
  5496. else
  5497. [item setValue:@"false" forKey:@"wish_exists"];
  5498. if(closeout==0)
  5499. [item setValue:@"false" forKey:@"is_closeout"];
  5500. else
  5501. [item setValue:@"true" forKey:@"is_closeout"];
  5502. if(cid==0)
  5503. [item setValue:@"false" forKey:@"cart_exists"];
  5504. else
  5505. [item setValue:@"true" forKey:@"cart_exists"];
  5506. [item addEntriesFromDictionary:imgjson];
  5507. // [item setValue:nsurl forKey:@"img"];
  5508. [item setValue:nsname forKey:@"fash_name"];
  5509. [item setValue:nsdescription forKey:@"description"];
  5510. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5511. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5512. i++;
  5513. }
  5514. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5515. [ret setObject:items forKey:@"items"];
  5516. sqlite3_finalize(statement);
  5517. }
  5518. [iSalesDB close_db:db];
  5519. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5520. return ret;
  5521. }
  5522. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5523. {
  5524. return [self itemsearch:params limited:YES];
  5525. }
  5526. #pragma mark order detail
  5527. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5528. if (str1.length == 0) {
  5529. str1 = @"&nbsp";
  5530. }
  5531. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5532. return str;
  5533. }
  5534. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5535. {
  5536. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5537. [fromformatter setDateFormat:from];
  5538. NSDate *date = [fromformatter dateFromString:datetime];
  5539. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5540. [toformatter setDateFormat:to];
  5541. NSString * ret = [toformatter stringFromDate:date];
  5542. return ret;
  5543. }
  5544. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5545. // 把毫秒去掉
  5546. if ([dateTime containsString:@"."]) {
  5547. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5548. }
  5549. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5550. formatter.dateFormat = formate;
  5551. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5552. NSDate *date = [formatter dateFromString:dateTime];
  5553. formatter.dateFormat = newFormate;
  5554. NSString *result = [formatter stringFromDate:date];
  5555. return result ? result : @"";
  5556. }
  5557. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5558. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5559. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5560. }
  5561. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5562. {
  5563. DebugLog(@"offline oderdetail params: %@",params);
  5564. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5565. int orderId = [params[@"orderId"] intValue];
  5566. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5567. // decrypt card number and card security code
  5568. // NSString* sql=[NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,o.TotalCuft,o.TotalWeight,o.TotalCarton,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),o.credit_card_expiration,o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus from (select _id,so_id,poNumber,create_time,status,TotalCuft,TotalWeight,TotalCarton,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId ];
  5569. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  5570. sqlite3 *db = [iSalesDB get_db];
  5571. sqlite3_stmt * statement;
  5572. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5573. NSString *nssoid = @"";
  5574. NSString* orderinfo = @"";
  5575. NSString *moreInfo = @"";
  5576. double handlingFee = 0;
  5577. double payments_and_credist = 0;
  5578. double totalPrice = 0;
  5579. double shippingFee = 0;
  5580. double lift_gate = 0;
  5581. NSString *customer_contact = @"";
  5582. NSString *customer_email = @"";
  5583. NSString *customer_fax = @"";
  5584. NSString *customer_phone = @"";
  5585. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5586. {
  5587. if (sqlite3_step(statement) == SQLITE_ROW)
  5588. {
  5589. int order_id = sqlite3_column_int(statement, 0);
  5590. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5591. ret[@"sign_url"] = sign_url;
  5592. customer_contact = [self textAtColumn:52 statement:statement];
  5593. customer_email = [self textAtColumn:53 statement:statement];
  5594. customer_phone = [self textAtColumn:54 statement:statement];
  5595. customer_fax = [self textAtColumn:55 statement:statement];
  5596. int offline_edit=sqlite3_column_int(statement, 56);
  5597. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5598. char *soid = (char*)sqlite3_column_text(statement, 1);
  5599. if(soid==nil)
  5600. soid= "";
  5601. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5602. // so#
  5603. ret[@"so#"] = nssoid;
  5604. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5605. if(poNumber==nil)
  5606. poNumber= "";
  5607. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5608. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5609. if(create_time==nil)
  5610. create_time= "";
  5611. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5612. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5613. int status = sqlite3_column_int(statement, 4);
  5614. int erpStatus = sqlite3_column_int(statement, 49);
  5615. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5616. // status
  5617. if (status > 1 && status != 3) {
  5618. status = erpStatus;
  5619. } else if (status == 3) {
  5620. status = 15;
  5621. }
  5622. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5623. ret[@"order_status"] = nsstatus;
  5624. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5625. if(company_name==nil)
  5626. company_name= "";
  5627. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5628. // company name
  5629. ret[@"company_name"] = nscompany_name;
  5630. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5631. if(customer_contact==nil)
  5632. customer_contact= "";
  5633. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5634. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5635. if(addr_1==nil)
  5636. addr_1="";
  5637. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5638. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5639. if(addr_2==nil)
  5640. addr_2="";
  5641. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5642. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5643. if(addr_3==nil)
  5644. addr_3="";
  5645. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5646. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5647. if(addr_4==nil)
  5648. addr_4="";
  5649. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5650. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5651. [arr_addr addObject:nsaddr_1];
  5652. [arr_addr addObject:nsaddr_2];
  5653. [arr_addr addObject:nsaddr_3];
  5654. [arr_addr addObject:nsaddr_4];
  5655. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  5656. char *logist = (char*)sqlite3_column_text(statement, 11);
  5657. if(logist==nil)
  5658. logist= "";
  5659. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5660. if (status == -11 || status == 10 || status == 11) {
  5661. nslogist = [self textAtColumn:59 statement:statement];
  5662. };
  5663. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5664. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5665. shipping = @"Shipping To Be Quoted";
  5666. } else {
  5667. shippingFee = sqlite3_column_double(statement, 12);
  5668. }
  5669. // Shipping
  5670. ret[@"Shipping"] = shipping;
  5671. int have_lift_gate = sqlite3_column_int(statement, 17);
  5672. lift_gate = sqlite3_column_double(statement, 13);
  5673. // Liftgate Fee(No loading dock)
  5674. if (!have_lift_gate) {
  5675. lift_gate = 0;
  5676. }
  5677. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  5678. if (sqlite3_column_int(statement, 57)) {
  5679. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  5680. }
  5681. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  5682. if(general_notes==nil)
  5683. general_notes= "";
  5684. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  5685. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  5686. if(internal_notes==nil)
  5687. internal_notes= "";
  5688. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  5689. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  5690. if(payment_type==nil)
  5691. payment_type= "";
  5692. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  5693. // order info
  5694. orderinfo = [self textFileName:@"order_info.html"];
  5695. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  5696. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  5697. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  5698. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  5699. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  5700. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  5701. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  5702. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  5703. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  5704. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  5705. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  5706. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  5707. NSString *payment = nil;
  5708. if([nspayment_type isEqualToString:@"Credit Card"])
  5709. {
  5710. payment = [self textFileName:@"creditcardpayment.html"];
  5711. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  5712. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  5713. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  5714. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  5715. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  5716. NSString *card_type = [self textAtColumn:42 statement:statement];
  5717. if (card_type.length > 0) { // 显示星号
  5718. card_type = @"****";
  5719. }
  5720. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  5721. if (card_number.length > 0 && card_number.length > 4) {
  5722. for (int i = 0; i < card_number.length - 4; i++) {
  5723. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  5724. }
  5725. } else {
  5726. card_number = @"";
  5727. }
  5728. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  5729. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  5730. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  5731. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  5732. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  5733. card_expiration = @"****";
  5734. }
  5735. NSString *card_city = [self textAtColumn:46 statement:statement];
  5736. NSString *card_state = [self textAtColumn:47 statement:statement];
  5737. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  5738. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  5739. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  5740. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  5741. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  5742. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  5743. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  5744. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  5745. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  5746. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  5747. }
  5748. else
  5749. {
  5750. payment=[self textFileName:@"normalpayment.html"];
  5751. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  5752. }
  5753. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  5754. ret[@"result"]= [NSNumber numberWithInt:2];
  5755. // more info
  5756. moreInfo = [self textFileName:@"more_info.html"];
  5757. /*****ship to******/
  5758. // ShipToCompany_or_&nbsp
  5759. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  5760. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  5761. NSString *shipToName = [self textAtColumn:19 statement:statement];
  5762. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  5763. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  5764. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  5765. /*****ship from******/
  5766. // ShipFromCompany_or_&nbsp
  5767. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  5768. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  5769. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  5770. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  5771. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  5772. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  5773. /*****freight to******/
  5774. // FreightBillToCompany_or_&nbsp
  5775. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  5776. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  5777. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  5778. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  5779. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  5780. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  5781. /*****merchandise to******/
  5782. // MerchandiseBillToCompany_or_&nbsp
  5783. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  5784. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  5785. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  5786. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  5787. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  5788. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  5789. /*****return to******/
  5790. // ReturnToCompany_or_&nbsp
  5791. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  5792. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  5793. NSString *returnToName = [self textAtColumn:31 statement:statement];
  5794. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  5795. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  5796. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  5797. //
  5798. // DebugLog(@"more info : %@",moreInfo);
  5799. // handling fee
  5800. handlingFee = sqlite3_column_double(statement, 33);
  5801. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  5802. if (sqlite3_column_int(statement, 58)) {
  5803. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  5804. }
  5805. //
  5806. // customer info
  5807. customerID = [self textAtColumn:36 statement:statement];
  5808. // mode
  5809. ret[@"mode"] = appDelegate.mode;
  5810. // model_count
  5811. ret[@"model_count"] = @(0);
  5812. }
  5813. sqlite3_finalize(statement);
  5814. }
  5815. [iSalesDB close_db:db];
  5816. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  5817. // "customer_email" = "Shui Hu";
  5818. // "customer_fax" = "";
  5819. // "customer_first_name" = F;
  5820. // "customer_last_name" = L;
  5821. // "customer_name" = ",da He Xiang Dong Liu A";
  5822. // "customer_phone" = "Hey Xuan Feng";
  5823. contactInfo[@"customer_phone"] = customer_phone;
  5824. contactInfo[@"customer_fax"] = customer_fax;
  5825. contactInfo[@"customer_email"] = customer_email;
  5826. NSString *first_name = @"";
  5827. NSString *last_name = @"";
  5828. if ([customer_contact isEqualToString:@""]) {
  5829. } else if ([customer_contact containsString:@" "]) {
  5830. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  5831. first_name = [customer_contact substringToIndex:first_space_index];
  5832. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  5833. }
  5834. contactInfo[@"customer_first_name"] = first_name;
  5835. contactInfo[@"customer_last_name"] = last_name;
  5836. ret[@"customerInfo"] = contactInfo;
  5837. // models
  5838. if (nssoid) {
  5839. __block double TotalCuft = 0;
  5840. __block double TotalWeight = 0;
  5841. __block int TotalCarton = 0;
  5842. __block double allItemPrice = 0;
  5843. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",nssoid];
  5844. sqlite3 *db1 = [iSalesDB get_db];
  5845. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5846. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5847. int product_id = sqlite3_column_int(stmt, 0);
  5848. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5849. int item_id = sqlite3_column_int(stmt, 7);
  5850. NSString* Price=nil;
  5851. if(str_price==nil)
  5852. {
  5853. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  5854. if(price==nil)
  5855. Price=@"No Price.";
  5856. else
  5857. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5858. }
  5859. else
  5860. {
  5861. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5862. }
  5863. double discount = sqlite3_column_double(stmt, 2);
  5864. int item_count = sqlite3_column_int(stmt, 3);
  5865. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  5866. NSString *nsline_note=nil;
  5867. if(line_note!=nil)
  5868. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  5869. // char *name = (char*)sqlite3_column_text(stmt, 5);
  5870. // NSString *nsname = nil;
  5871. // if(name!=nil)
  5872. // nsname= [[NSString alloc]initWithUTF8String:name];
  5873. NSString *nsname = [self textAtColumn:5 statement:stmt];
  5874. // char *description = (char*)sqlite3_column_text(stmt, 6);
  5875. // NSString *nsdescription=nil;
  5876. // if(description!=nil)
  5877. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  5878. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  5879. // int stockUom = sqlite3_column_int(stmt, 8);
  5880. // int _id = sqlite3_column_int(stmt, 9);
  5881. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  5882. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  5883. double weight=[bsubtotaljson[@"weight"] doubleValue];
  5884. int carton=[bsubtotaljson[@"carton"] intValue];
  5885. TotalCuft += cuft;
  5886. TotalWeight += weight;
  5887. TotalCarton += carton;
  5888. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  5889. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  5890. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  5891. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  5892. itemjson[@"note"]=nsline_note;
  5893. itemjson[@"origin_price"] = Price;
  5894. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5895. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5896. itemjson[@"order_item_status"] = @""; // 暂时不处理
  5897. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  5898. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  5899. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  5900. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5901. if(itemjson[@"combine"] != nil)
  5902. {
  5903. // int citem=0;
  5904. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  5905. for(int bc=0;bc<bcount;bc++)
  5906. {
  5907. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5908. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5909. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5910. subTotal += uprice * modulus * item_count;
  5911. }
  5912. }
  5913. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  5914. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  5915. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  5916. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  5917. allItemPrice += subTotal;
  5918. }];
  5919. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  5920. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  5921. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  5922. // payments/Credits
  5923. // payments_and_credist = sqlite3_column_double(statement, 34);
  5924. payments_and_credist = allItemPrice;
  5925. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  5926. // // total
  5927. // totalPrice = sqlite3_column_double(statement, 35);
  5928. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  5929. } else {
  5930. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  5931. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  5932. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  5933. // payments/Credits
  5934. payments_and_credist = 0;
  5935. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  5936. }
  5937. // total
  5938. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  5939. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  5940. ret[@"order_info"]= orderinfo;
  5941. ret[@"more_order_info"] = moreInfo;
  5942. return [RAUtils dict2data:ret];
  5943. }
  5944. #pragma mark order list
  5945. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  5946. NSString *nsstatus = @"";
  5947. switch (status) {
  5948. case 0:
  5949. {
  5950. nsstatus=@"Temp Order";
  5951. break;
  5952. }
  5953. case 1:
  5954. {
  5955. nsstatus=@"Saved Order";
  5956. break;
  5957. }
  5958. case 2: {
  5959. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  5960. break;
  5961. }
  5962. case 3:
  5963. case 15:
  5964. {
  5965. nsstatus=@"Cancelled";
  5966. break;
  5967. }
  5968. case 10:
  5969. {
  5970. nsstatus=@"Quote Submitted";
  5971. break;
  5972. }
  5973. case 11:
  5974. {
  5975. nsstatus=@"Sales Order Submitted";
  5976. break;
  5977. }
  5978. case 12:
  5979. {
  5980. nsstatus=@"Processing";
  5981. break;
  5982. }
  5983. case 13:
  5984. {
  5985. nsstatus=@"Shipped";
  5986. break;
  5987. }
  5988. case 14:
  5989. {
  5990. nsstatus=@"Closed";
  5991. break;
  5992. }
  5993. case -11:
  5994. {
  5995. nsstatus = @"Ready For Submit";
  5996. break;
  5997. }
  5998. default:
  5999. break;
  6000. }
  6001. return nsstatus;
  6002. }
  6003. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  6004. double total = 0;
  6005. __block double payments_and_credist = 0;
  6006. __block double allItemPrice = 0;
  6007. // sqlite3 *db1 = [iSalesDB get_db];
  6008. if (so_id) {
  6009. // NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",so_id];
  6010. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id from (select product_id,decrypt(str_price) as price,discount,item_count,item_id from offline_cart where so_no='%@');",so_id];
  6011. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6012. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6013. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6014. int product_id = sqlite3_column_int(stmt, 0);
  6015. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6016. int discount = sqlite3_column_int(stmt, 2);
  6017. int item_count = sqlite3_column_int(stmt, 3);
  6018. // int item_id = sqlite3_column_int(stmt, 7);
  6019. int item_id = sqlite3_column_int(stmt, 4);
  6020. NSString* Price=nil;
  6021. if(str_price==nil)
  6022. {
  6023. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  6024. if(price==nil)
  6025. Price=@"No Price.";
  6026. else
  6027. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6028. }
  6029. else
  6030. {
  6031. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6032. }
  6033. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  6034. itemjson[@"The unit price"]=Price;
  6035. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6036. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6037. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6038. if(itemjson[@"combine"] != nil)
  6039. {
  6040. // int citem=0;
  6041. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6042. for(int bc=0;bc<bcount;bc++)
  6043. {
  6044. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6045. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6046. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6047. subTotal += uprice * modulus * item_count;
  6048. }
  6049. }
  6050. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6051. allItemPrice += subTotal;
  6052. }];
  6053. payments_and_credist = allItemPrice;
  6054. } else {
  6055. // payments/Credits
  6056. payments_and_credist = 0;
  6057. }
  6058. // lift_gate handlingFee shippingFee
  6059. __block double lift_gate = 0;
  6060. __block double handlingFee = 0;
  6061. __block double shippingFee = 0;
  6062. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6063. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6064. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6065. if (have_lift_gate) {
  6066. lift_gate = sqlite3_column_double(stmt, 1);
  6067. }
  6068. handlingFee = sqlite3_column_double(stmt, 2);
  6069. shippingFee = sqlite3_column_double(stmt, 3);
  6070. }];
  6071. // total
  6072. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  6073. if (close) {
  6074. [iSalesDB close_db:db1];
  6075. }
  6076. return total;
  6077. }
  6078. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6079. {
  6080. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6081. int limit = [[params valueForKey:@"limit"] intValue];
  6082. int offset = [[params valueForKey:@"offset"] intValue];
  6083. NSString* keyword = [params valueForKey:@"keyWord"];
  6084. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6085. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6086. NSString* where=@"1 = 1";
  6087. if(keyword.length>0)
  6088. where=[where stringByAppendingString:[NSString stringWithFormat:@" and (lower(o.so_id) like '%%%@%%' or lower(o.sales_rep) like '%%%@%%' or lower(decrypt(c.company_name)) like '%%%@%%' or lower(o.create_by) like '%%%@%%')",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString]];
  6089. if (orderStatus.length > 0) {
  6090. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6091. if (order_status_array.count == 1) {
  6092. int status_value = [[order_status_array firstObject] integerValue];
  6093. if (status_value <= 1 || status_value == 3) {
  6094. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6095. } else {
  6096. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6097. }
  6098. } else if (order_status_array.count > 1) {
  6099. for (int i = 0; i < order_status_array.count;i++) {
  6100. NSString *status = order_status_array[i];
  6101. NSString *condition = @" or";
  6102. if (i == 0) {
  6103. condition = @" and (";
  6104. }
  6105. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6106. int status_value = [status integerValue];
  6107. if (status_value <= 1 || status_value == 3) {
  6108. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6109. } else {
  6110. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6111. }
  6112. }
  6113. where = [where stringByAppendingString:@" )"];
  6114. }
  6115. }
  6116. NSString* sqlQuery=[NSString stringWithFormat:@"SELECT o._id,o.so_id,o.status,o.sales_rep,o.create_by,decrypt(c.company_name),o.create_time,o.total_price,o.customer_contact,o.erpOrderStatus,o.order_id is null from offline_order o left join offline_contact c on o.customer_cid=c.contact_id where %@ order by o.create_time desc limit %d offset %d" ,where, limit, offset];
  6117. // DebugLog(@"order list sql: %@",sqlQuery);
  6118. sqlite3 *db = [iSalesDB get_db];
  6119. sqlite3_stmt * statement;
  6120. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6121. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6122. {
  6123. int count=0;
  6124. while (sqlite3_step(statement) == SQLITE_ROW)
  6125. {
  6126. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6127. int order_id = sqlite3_column_double(statement, 0);
  6128. char *soid = (char*)sqlite3_column_text(statement, 1);
  6129. if(soid==nil)
  6130. soid= "";
  6131. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6132. int status = sqlite3_column_double(statement, 2);
  6133. int erpStatus = sqlite3_column_double(statement, 9);
  6134. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6135. if(sales_rep==nil)
  6136. sales_rep= "";
  6137. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6138. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6139. if(create_by==nil)
  6140. create_by= "";
  6141. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6142. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6143. if(company_name==nil)
  6144. company_name= "";
  6145. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6146. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6147. if(create_time==nil)
  6148. create_time= "";
  6149. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6150. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6151. // double total_price = sqlite3_column_double(statement, 7);
  6152. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  6153. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6154. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6155. int offline_edit = sqlite3_column_int(statement, 10);
  6156. // ": "JH",
  6157. // "": "$8307.00",
  6158. // "": "MOB1608050001",
  6159. // "": "ArpithaT",
  6160. // "": "1st Stage Property Transformations",
  6161. // "": "JANICE SUTTON",
  6162. // "": 2255,
  6163. // "": "08/02/2016 09:49:18",
  6164. // "": 1,
  6165. // "": "Saved Order"
  6166. // "": "1470384050483",
  6167. // "model_count": "6 / 28"
  6168. item[@"sales_rep"]= nssales_rep;
  6169. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6170. item[@"so#"]= nssoid;
  6171. item[@"create_by"]= nscreate_by;
  6172. item[@"customer_name"]= nscompany_name;
  6173. item[@"customer_contact"] = customer_contact;
  6174. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6175. item[@"purchase_time"]= nscreate_time;
  6176. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  6177. item[@"order_status"]= nsstatus;
  6178. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6179. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6180. // item[@"model_count"]
  6181. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6182. count++;
  6183. }
  6184. ret[@"count"]= [NSNumber numberWithInt:count];
  6185. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6186. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6187. ret[@"result"]= [NSNumber numberWithInt:2];
  6188. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6189. ret[@"time_zone"] = @"PST";
  6190. sqlite3_finalize(statement);
  6191. }
  6192. int total_count = [iSalesDB get_recordcount:db table:@"offline_order o left join offline_contact c on o.customer_cid=c.contact_id" where:where];
  6193. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6194. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6195. [iSalesDB close_db:db];
  6196. return [RAUtils dict2data:ret];
  6197. }
  6198. #pragma mark update gnotes
  6199. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6200. {
  6201. DebugLog(@"params: %@",params);
  6202. // comments = Meyoyoyoyoyoyoy;
  6203. // orderCode = MOB1608110001;
  6204. // password = 123456;
  6205. // user = EvanK;
  6206. NSString *sql = [NSString stringWithFormat:@"update offline_order set general_notes = '%@' where so_id = '%@';",[self valueInParams:params key:@"comments" translateSingleQuote:YES],[self valueInParams:params key:@"orderCode" translateSingleQuote:YES]];
  6207. int ret = [iSalesDB execSql:sql];
  6208. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6209. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6210. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6211. // [dic setValue:@"160409" forKey:@"min_ver"];
  6212. return [RAUtils dict2data:dic];
  6213. }
  6214. #pragma mark move to wishlist
  6215. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6216. {
  6217. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6218. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6219. _id = [NSString stringWithFormat:@"(%@)",_id];
  6220. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6221. sqlite3 *db = [iSalesDB get_db];
  6222. __block NSString *product_id = @"";
  6223. __block NSString *item_count_str = @"";
  6224. // __block NSString *item_id = nil;
  6225. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6226. // product_id = [self textAtColumn:0 statement:stmt];
  6227. int item_count = sqlite3_column_int(stmt, 1);
  6228. // item_id = [self textAtColumn:2 statement:stmt];
  6229. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6230. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6231. if (p_id.length) {
  6232. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6233. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6234. }
  6235. }];
  6236. [iSalesDB close_db:db];
  6237. // 去除第一个,
  6238. product_id = [product_id substringFromIndex:1];
  6239. item_count_str = [item_count_str substringFromIndex:1];
  6240. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6241. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6242. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6243. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6244. sqlite3 *db1 = [iSalesDB get_db];
  6245. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6246. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6247. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6248. // 删除
  6249. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6250. int ret = [iSalesDB execSql:deleteSql db:db1];
  6251. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6252. [iSalesDB close_db:db1];
  6253. return [RAUtils dict2data:dic];
  6254. }
  6255. #pragma mark cart delete
  6256. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6257. {
  6258. // cartItemId = 548;
  6259. // orderCode = MOB1608110001;
  6260. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6261. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6262. _id = [NSString stringWithFormat:@"(%@)",_id];
  6263. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6264. int ret = [iSalesDB execSql:sql];
  6265. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6266. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6267. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6268. // [dic setValue:@"160409" forKey:@"min_ver"];
  6269. return [RAUtils dict2data:dic];
  6270. }
  6271. #pragma mark set price
  6272. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6273. {
  6274. DebugLog(@"cart set price params: %@",params);
  6275. // "cartitem_id" = 1;
  6276. // discount = "0.000000";
  6277. // "item_note" = "";
  6278. // price = "269.000000";
  6279. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6280. NSString *notes = [self valueInParams:params key:@"item_note"];
  6281. NSString *discount = [self valueInParams:params key:@"discount"];
  6282. NSString *price = [self valueInParams:params key:@"price"];
  6283. // bool badd_price_changed=false;
  6284. // sqlite3* db=[iSalesDB get_db];
  6285. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6286. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6287. // NSRange range;
  6288. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6289. //
  6290. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6291. // badd_price_changed=true;
  6292. // [iSalesDB close_db:db];
  6293. //
  6294. // if(badd_price_changed)
  6295. // {
  6296. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6297. // }
  6298. //
  6299. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6300. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6301. int ret = [iSalesDB execSql:sql];
  6302. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6303. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6304. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6305. // [dic setValue:@"160409" forKey:@"min_ver"];
  6306. return [RAUtils dict2data:dic];
  6307. }
  6308. #pragma mark set line notes
  6309. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6310. {
  6311. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6312. NSString *notes = [self valueInParams:params key:@"notes"];
  6313. notes = [self translateSingleQuote:notes];
  6314. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6315. int ret = [iSalesDB execSql:sql];
  6316. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6317. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6318. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6319. // [dic setValue:@"160409" forKey:@"min_ver"];
  6320. return [RAUtils dict2data:dic];
  6321. }
  6322. #pragma mark set qty
  6323. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6324. {
  6325. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6326. int item_count = [params[@"inputInt"] integerValue];
  6327. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6328. int ret = [iSalesDB execSql:sql];
  6329. __block int item_id = 0;
  6330. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6331. item_id = sqlite3_column_int(stmt, 0);
  6332. }];
  6333. sqlite3 *db = [iSalesDB get_db];
  6334. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6335. [iSalesDB close_db:db];
  6336. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6337. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6338. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6339. // [dic setValue:@"160409" forKey:@"min_ver"];
  6340. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6341. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6342. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6343. return [RAUtils dict2data:dic];
  6344. }
  6345. #pragma mark place order
  6346. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6347. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6348. if (aname) {
  6349. [dic setValue:aname forKey:@"aname"];
  6350. }
  6351. if (control) {
  6352. [dic setValue:control forKey:@"control"];
  6353. }
  6354. if (keyboard) {
  6355. [dic setValue:keyboard forKey:@"keyboard"];
  6356. }
  6357. if (name) {
  6358. [dic setValue:name forKey:@"name"];
  6359. }
  6360. if (value) {
  6361. [dic setValue:value forKey:@"value"];
  6362. }
  6363. return dic;
  6364. }
  6365. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6366. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6367. orderCode = [self translateSingleQuote:orderCode];
  6368. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6369. NSString *customerSql = [NSString stringWithFormat:@"select c.contact_id,c.img_0,c.img_1,c.img_2,decrypt(c.company_name),decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,c.first_name,c.last_name,o.customer_email,o.customer_phone,o.customer_fax,c.zipcode,c.city,c.state,c.country,o.customer_contact from offline_contact as c join offline_order as o on c.contact_id = o.customer_cid where o.so_id = '%@';",orderCode];
  6370. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6371. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6372. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6373. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6374. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6375. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6376. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6377. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6378. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6379. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6380. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6381. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6382. NSString *email = [self textAtColumn:11 statement:stmt];
  6383. NSString *phone = [self textAtColumn:12 statement:stmt];
  6384. NSString *fax = [self textAtColumn:13 statement:stmt];
  6385. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6386. NSString *city = [self textAtColumn:15 statement:stmt];
  6387. NSString *state = [self textAtColumn:16 statement:stmt];
  6388. NSString *country = [self textAtColumn:17 statement:stmt];
  6389. NSString *name = [self textAtColumn:18 statement:stmt];
  6390. // contact id
  6391. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6392. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6393. [contact_id_dic setValue:@"text" forKey:@"control"];
  6394. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6395. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6396. [contact_id_dic setValue:@"true" forKey:@"required"];
  6397. [contact_id_dic setValue:contact_id forKey:@"value"];
  6398. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6399. // business card
  6400. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6401. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6402. [business_card_dic setValue:@"img" forKey:@"control"];
  6403. [business_card_dic setValue:@"1" forKey:@"disable"];
  6404. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6405. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6406. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6407. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6408. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6409. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6410. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6411. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6412. // fax
  6413. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6414. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6415. // zipcode
  6416. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6417. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6418. // city
  6419. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6420. [customer_dic setValue:city_dic forKey:@"item_12"];
  6421. // state
  6422. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6423. [customer_dic setValue:state_dic forKey:@"item_13"];
  6424. // country
  6425. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6426. [customer_dic setValue:country_dic forKey:@"item_14"];
  6427. // company name
  6428. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6429. [customer_dic setValue:company_dic forKey:@"item_2"];
  6430. // addr_1
  6431. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6432. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6433. // addr_2
  6434. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6435. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6436. // addr_3
  6437. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6438. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6439. // addr_4
  6440. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6441. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6442. // Contact
  6443. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6444. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6445. // email
  6446. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6447. [customer_dic setValue:email_dic forKey:@"item_8"];
  6448. // phone
  6449. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6450. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6451. // title
  6452. [customer_dic setValue:@"Customer" forKey:@"title"];
  6453. // count
  6454. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6455. }];
  6456. // setting
  6457. NSDictionary *setting = params[@"setting"];
  6458. NSNumber *hide = setting[@"CustomerHide"];
  6459. [customer_dic setValue:hide forKey:@"hide"];
  6460. return customer_dic;
  6461. }
  6462. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6463. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6464. // setting
  6465. NSDictionary *setting = params[@"setting"];
  6466. NSNumber *hide = setting[@"ShipToHide"];
  6467. [dic setValue:hide forKey:@"hide"];
  6468. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6469. orderCode = [self translateSingleQuote:orderCode];
  6470. NSString *sql = [NSString stringWithFormat:@"select receive_cid,receive_name,receive_ext,receive_contact,receive_email,receive_fax,receive_phone from offline_order where so_id = '%@';",orderCode];
  6471. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6472. NSString *cid = [self textAtColumn:0 statement:stmt];
  6473. NSString *name = [self textAtColumn:1 statement:stmt];
  6474. NSString *ext = [self textAtColumn:2 statement:stmt];
  6475. NSString *contact = [self textAtColumn:3 statement:stmt];
  6476. NSString *email = [self textAtColumn:4 statement:stmt];
  6477. NSString *fax = [self textAtColumn:5 statement:stmt];
  6478. NSString *phone = [self textAtColumn:6 statement:stmt];
  6479. // count
  6480. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6481. // title
  6482. [dic setValue:@"Ship To" forKey:@"title"];
  6483. // choose
  6484. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6485. [choose_dic setValue:@"choose" forKey:@"aname"];
  6486. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6487. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6488. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6489. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6490. @"receive_contact" : @"customer_contact",
  6491. @"receive_email" : @"customer_email",
  6492. @"receive_ext" : @"customer_contact_ext",
  6493. @"receive_fax" : @"customer_fax",
  6494. @"receive_name" : @"customer_name",
  6495. @"receive_phone" : @"customer_phone"},
  6496. @"refresh" : [NSNumber numberWithInteger:1],
  6497. @"type" : @"pull"};
  6498. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6499. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6500. @"name" : @"Add new address",
  6501. @"refresh" : [NSNumber numberWithInteger:1],
  6502. @"value" : @"new_addr"
  6503. };
  6504. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6505. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6506. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6507. @"receive_contact" : @"customer_contact",
  6508. @"receive_email" : @"customer_email",
  6509. @"receive_ext" : @"customer_contact_ext",
  6510. @"receive_fax" : @"customer_fax",
  6511. @"receive_name" : @"customer_name",
  6512. @"receive_phone" : @"customer_phone"},
  6513. @"name" : @"select_ship_to",
  6514. @"refresh" : [NSNumber numberWithInteger:1],
  6515. @"value" : @"Sales_Order_Ship_To"};
  6516. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6517. [dic setValue:choose_dic forKey:@"item_0"];
  6518. // contact id
  6519. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6520. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6521. [contact_id_dic setValue:@"true" forKey:@"required"];
  6522. [dic setValue:contact_id_dic forKey:@"item_1"];
  6523. // company name
  6524. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6525. [dic setValue:company_name_dic forKey:@"item_2"];
  6526. // address
  6527. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6528. NSNumber *required = setting[@"ShippingToAddressRequire"];
  6529. if ([required integerValue]) {
  6530. [ext_dic setValue:@"true" forKey:@"required"];
  6531. }
  6532. [dic setValue:ext_dic forKey:@"item_3"];
  6533. // contact
  6534. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6535. [dic setValue:contact_dic forKey:@"item_4"];
  6536. // phone
  6537. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6538. [dic setValue:phone_dic forKey:@"item_5"];
  6539. // fax
  6540. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6541. [dic setValue:fax_dic forKey:@"item_6"];
  6542. // email
  6543. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6544. [dic setValue:email_dic forKey:@"item_7"];
  6545. }];
  6546. return dic;
  6547. }
  6548. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  6549. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6550. // setting
  6551. NSDictionary *setting = params[@"setting"];
  6552. NSNumber *hide = setting[@"ShipFromHide"];
  6553. [dic setValue:hide forKey:@"hide"];
  6554. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6555. orderCode = [self translateSingleQuote:orderCode];
  6556. NSString *sql = [NSString stringWithFormat:@"select sender_cid,sender_name,sender_ext,sender_contact,sender_email,sender_fax,sender_phone from offline_order where so_id = '%@';",orderCode];
  6557. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,phone from offline_contact where contact_id = 'NPD';";
  6558. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6559. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  6560. __block NSString *name = [self textAtColumn:1 statement:stmt];
  6561. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  6562. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  6563. __block NSString *email = [self textAtColumn:4 statement:stmt];
  6564. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  6565. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  6566. if (!cid.length) {
  6567. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  6568. cid = [self textAtColumn:0 statement:statment];
  6569. name = [self textAtColumn:1 statement:statment];
  6570. ext = [self textAtColumn:2 statement:statment];
  6571. contact = [self textAtColumn:3 statement:statment];
  6572. email = [self textAtColumn:4 statement:statment];
  6573. fax = [self textAtColumn:5 statement:statment];
  6574. phone = [self textAtColumn:6 statement:statment];
  6575. }];
  6576. }
  6577. // count
  6578. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6579. // title
  6580. [dic setValue:@"Ship From" forKey:@"title"];
  6581. // hide
  6582. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6583. // choose
  6584. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6585. [choose_dic setValue:@"choose" forKey:@"aname"];
  6586. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6587. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6588. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  6589. @"key_map" : @{@"sender_cid" : @"customer_cid",
  6590. @"sender_contact" : @"customer_contact",
  6591. @"sender_email" : @"customer_email",
  6592. @"sender_ext" : @"customer_contact_ext",
  6593. @"sender_fax" : @"customer_fax",
  6594. @"sender_name" : @"customer_name",
  6595. @"sender_phone" : @"customer_phone"},
  6596. @"name" : @"select_cid",
  6597. @"refresh" : [NSNumber numberWithInteger:0],
  6598. @"value" : @"Sales_Order_Ship_From"};
  6599. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  6600. [dic setValue:choose_dic forKey:@"item_0"];
  6601. // contact id
  6602. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6603. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6604. [contact_id_dic setValue:@"true" forKey:@"required"];
  6605. [dic setValue:contact_id_dic forKey:@"item_1"];
  6606. // company name
  6607. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6608. [dic setValue:company_name_dic forKey:@"item_2"];
  6609. // address
  6610. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6611. [dic setValue:ext_dic forKey:@"item_3"];
  6612. // contact
  6613. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6614. [dic setValue:contact_dic forKey:@"item_4"];
  6615. // phone
  6616. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6617. [dic setValue:phone_dic forKey:@"item_5"];
  6618. // fax
  6619. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6620. [dic setValue:fax_dic forKey:@"item_6"];
  6621. // email
  6622. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6623. [dic setValue:email_dic forKey:@"item_7"];
  6624. }];
  6625. return dic;
  6626. }
  6627. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  6628. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6629. // setting
  6630. NSDictionary *setting = params[@"setting"];
  6631. NSNumber *hide = setting[@"FreightBillToHide"];
  6632. [dic setValue:hide forKey:@"hide"];
  6633. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6634. orderCode = [self translateSingleQuote:orderCode];
  6635. NSString *sql = [NSString stringWithFormat:@"select shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_email,shipping_billto_fax,shipping_billto_phone from offline_order where so_id = '%@';",orderCode];
  6636. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6637. NSString *cid = [self textAtColumn:0 statement:stmt];
  6638. NSString *name = [self textAtColumn:1 statement:stmt];
  6639. NSString *ext = [self textAtColumn:2 statement:stmt];
  6640. NSString *contact = [self textAtColumn:3 statement:stmt];
  6641. NSString *email = [self textAtColumn:4 statement:stmt];
  6642. NSString *fax = [self textAtColumn:5 statement:stmt];
  6643. NSString *phone = [self textAtColumn:6 statement:stmt];
  6644. // count
  6645. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6646. // title
  6647. [dic setValue:@"Freight Bill To" forKey:@"title"];
  6648. // hide
  6649. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6650. // choose
  6651. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6652. [choose_dic setValue:@"choose" forKey:@"aname"];
  6653. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6654. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6655. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6656. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  6657. @"shipping_billto_contact" : @"receive_contact",
  6658. @"shipping_billto_email" : @"receive_email",
  6659. @"shipping_billto_ext" : @"receive_ext",
  6660. @"shipping_billto_fax" : @"receive_fax",
  6661. @"shipping_billto_name" : @"receive_name",
  6662. @"shipping_billto_phone" : @"receive_phone"},
  6663. @"type" : @"pull"};
  6664. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6665. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6666. @"type" : @"pull",
  6667. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  6668. @"shipping_billto_contact" : @"customer_contact",
  6669. @"shipping_billto_email" : @"customer_email",
  6670. @"shipping_billto_ext" : @"customer_contact_ext",
  6671. @"shipping_billto_fax" : @"customer_fax",
  6672. @"shipping_billto_name" : @"customer_name",
  6673. @"shipping_billto_phone" : @"customer_phone"}
  6674. };
  6675. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6676. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6677. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  6678. @"shipping_billto_contact" : @"sender_contact",
  6679. @"shipping_billto_email" : @"sender_email",
  6680. @"shipping_billto_ext" : @"sender_ext",
  6681. @"shipping_billto_fax" : @"sender_fax",
  6682. @"shipping_billto_name" : @"sender_name",
  6683. @"shipping_billto_phone" : @"sender_phone"},
  6684. @"type" : @"pull"
  6685. };
  6686. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  6687. NSDictionary *select_freight_bill_to_dic = @{
  6688. @"aname" : @"Select freight bill to",
  6689. @"name" : @"select_cid",
  6690. @"refresh" : [NSNumber numberWithInteger:0],
  6691. @"value" : @"Sales_Order_Freight_Bill_To",
  6692. @"key_map" : @{
  6693. @"shipping_billto_cid" : @"customer_cid",
  6694. @"shipping_billto_contact" : @"customer_contact",
  6695. @"shipping_billto_email" : @"customer_email",
  6696. @"shipping_billto_ext" : @"customer_contact_ext",
  6697. @"shipping_billto_fax" : @"customer_fax",
  6698. @"shipping_billto_name" : @"customer_name",
  6699. @"shipping_billto_phone" : @"customer_phone"
  6700. }
  6701. };
  6702. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  6703. [dic setValue:choose_dic forKey:@"item_0"];
  6704. // contact id
  6705. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6706. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6707. [contact_id_dic setValue:@"true" forKey:@"required"];
  6708. [dic setValue:contact_id_dic forKey:@"item_1"];
  6709. // company name
  6710. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6711. [dic setValue:company_name_dic forKey:@"item_2"];
  6712. // address
  6713. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6714. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  6715. if ([ext_required integerValue]) {
  6716. [ext_dic setValue:@"true" forKey:@"required"];
  6717. }
  6718. [dic setValue:ext_dic forKey:@"item_3"];
  6719. // contact
  6720. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6721. [dic setValue:contact_dic forKey:@"item_4"];
  6722. // phone
  6723. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6724. [dic setValue:phone_dic forKey:@"item_5"];
  6725. // fax
  6726. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6727. [dic setValue:fax_dic forKey:@"item_6"];
  6728. // email
  6729. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6730. [dic setValue:email_dic forKey:@"item_7"];
  6731. }];
  6732. return dic;
  6733. }
  6734. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6735. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6736. // setting
  6737. NSDictionary *setting = params[@"setting"];
  6738. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  6739. [dic setValue:hide forKey:@"hide"];
  6740. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6741. orderCode = [self translateSingleQuote:orderCode];
  6742. NSString *sql = [NSString stringWithFormat:@"select billing_cid,billing_name,billing_ext,billing_contact,billing_email,billing_fax,billing_phone from offline_order where so_id = '%@';",orderCode];
  6743. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6744. NSString *cid = [self textAtColumn:0 statement:stmt];
  6745. NSString *name = [self textAtColumn:1 statement:stmt];
  6746. NSString *ext = [self textAtColumn:2 statement:stmt];
  6747. NSString *contact = [self textAtColumn:3 statement:stmt];
  6748. NSString *email = [self textAtColumn:4 statement:stmt];
  6749. NSString *fax = [self textAtColumn:5 statement:stmt];
  6750. NSString *phone = [self textAtColumn:6 statement:stmt];
  6751. // count
  6752. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6753. // title
  6754. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  6755. // hide
  6756. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6757. // choose
  6758. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6759. [choose_dic setValue:@"choose" forKey:@"aname"];
  6760. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6761. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6762. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6763. @"key_map" : @{@"billing_cid" : @"receive_cid",
  6764. @"billing_contact" : @"receive_contact",
  6765. @"billing_email" : @"receive_email",
  6766. @"billing_ext" : @"receive_ext",
  6767. @"billing_fax" : @"receive_fax",
  6768. @"billing_name" : @"receive_name",
  6769. @"billing_phone" : @"receive_phone"},
  6770. @"type" : @"pull"};
  6771. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6772. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6773. @"type" : @"pull",
  6774. @"key_map" : @{@"billing_cid" : @"customer_cid",
  6775. @"billing_contact" : @"customer_contact",
  6776. @"billing_email" : @"customer_email",
  6777. @"billing_ext" : @"customer_contact_ext",
  6778. @"billing_fax" : @"customer_fax",
  6779. @"billing_name" : @"customer_name",
  6780. @"billing_phone" : @"customer_phone"}
  6781. };
  6782. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6783. NSDictionary *select_bill_to_dic = @{
  6784. @"aname" : @"Select bill to",
  6785. @"name" : @"select_cid",
  6786. @"refresh" : [NSNumber numberWithInteger:0],
  6787. @"value" : @"Sales_Order_Merchandise_Bill_To",
  6788. @"key_map" : @{
  6789. @"billing_cid" : @"customer_cid",
  6790. @"billing_contact" : @"customer_contact",
  6791. @"billing_email" : @"customer_email",
  6792. @"billing_ext" : @"customer_contact_ext",
  6793. @"billing_fax" : @"customer_fax",
  6794. @"billing_name" : @"customer_name",
  6795. @"billing_phone" : @"customer_phone"
  6796. }
  6797. };
  6798. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  6799. [dic setValue:choose_dic forKey:@"item_0"];
  6800. // contact id
  6801. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6802. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6803. [contact_id_dic setValue:@"true" forKey:@"required"];
  6804. [dic setValue:contact_id_dic forKey:@"item_1"];
  6805. // company name
  6806. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6807. [dic setValue:company_name_dic forKey:@"item_2"];
  6808. // address
  6809. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6810. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  6811. if ([ext_required integerValue]) {
  6812. [ext_dic setValue:@"true" forKey:@"required"];
  6813. }
  6814. [dic setValue:ext_dic forKey:@"item_3"];
  6815. // contact
  6816. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6817. [dic setValue:contact_dic forKey:@"item_4"];
  6818. // phone
  6819. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6820. [dic setValue:phone_dic forKey:@"item_5"];
  6821. // fax
  6822. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6823. [dic setValue:fax_dic forKey:@"item_6"];
  6824. // email
  6825. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6826. [dic setValue:email_dic forKey:@"item_7"];
  6827. }];
  6828. return dic;
  6829. }
  6830. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6831. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6832. // setting
  6833. NSDictionary *setting = params[@"setting"];
  6834. NSNumber *hide = setting[@"ReturnToHide"];
  6835. [dic setValue:hide forKey:@"hide"];
  6836. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6837. orderCode = [self translateSingleQuote:orderCode];
  6838. NSString *sql = [NSString stringWithFormat:@"select returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_email,returnto_fax,returnto_phone from offline_order where so_id = '%@';",orderCode];
  6839. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6840. NSString *cid = [self textAtColumn:0 statement:stmt];
  6841. NSString *name = [self textAtColumn:1 statement:stmt];
  6842. NSString *ext = [self textAtColumn:2 statement:stmt];
  6843. NSString *contact = [self textAtColumn:3 statement:stmt];
  6844. NSString *email = [self textAtColumn:4 statement:stmt];
  6845. NSString *fax = [self textAtColumn:5 statement:stmt];
  6846. NSString *phone = [self textAtColumn:6 statement:stmt];
  6847. // count
  6848. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6849. // title
  6850. [dic setValue:@"Return To" forKey:@"title"];
  6851. // hide
  6852. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6853. // choose
  6854. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6855. [choose_dic setValue:@"choose" forKey:@"aname"];
  6856. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6857. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  6858. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6859. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  6860. @"returnto_contact" : @"sender_contact",
  6861. @"returnto_email" : @"sender_email",
  6862. @"returnto_ext" : @"sender_ext",
  6863. @"returnto_fax" : @"sender_fax",
  6864. @"returnto_name" : @"sender_name",
  6865. @"returnto_phone" : @"sender_phone"},
  6866. @"type" : @"pull"};
  6867. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  6868. NSDictionary *select_return_to_dic = @{
  6869. @"aname" : @"Select return to",
  6870. @"name" : @"select_cid",
  6871. @"refresh" : [NSNumber numberWithInteger:0],
  6872. @"value" : @"Contact_Return_To",
  6873. @"key_map" : @{
  6874. @"returnto_cid" : @"customer_cid",
  6875. @"returnto_contact" : @"customer_contact",
  6876. @"returnto_email" : @"customer_email",
  6877. @"returnto_ext" : @"customer_contact_ext",
  6878. @"returnto_fax" : @"customer_fax",
  6879. @"returnto_name" : @"customer_name",
  6880. @"returnto_phone" : @"customer_phone"
  6881. }
  6882. };
  6883. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  6884. [dic setValue:choose_dic forKey:@"item_0"];
  6885. // contact id
  6886. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6887. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6888. [contact_id_dic setValue:@"true" forKey:@"required"];
  6889. [dic setValue:contact_id_dic forKey:@"item_1"];
  6890. // company name
  6891. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6892. [dic setValue:company_name_dic forKey:@"item_2"];
  6893. // address
  6894. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6895. [dic setValue:ext_dic forKey:@"item_3"];
  6896. // contact
  6897. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6898. [dic setValue:contact_dic forKey:@"item_4"];
  6899. // phone
  6900. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6901. [dic setValue:phone_dic forKey:@"item_5"];
  6902. // fax
  6903. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6904. [dic setValue:fax_dic forKey:@"item_6"];
  6905. // email
  6906. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6907. [dic setValue:email_dic forKey:@"item_7"];
  6908. }];
  6909. return dic;
  6910. }
  6911. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6912. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6913. orderCode = [self translateSingleQuote:orderCode];
  6914. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6915. __block double TotalCuft = 0;
  6916. __block double TotalWeight = 0;
  6917. __block int TotalCarton = 0;
  6918. __block double payments = 0;
  6919. // setting
  6920. NSDictionary *setting = params[@"setting"];
  6921. NSNumber *hide = setting[@"ModelInformationHide"];
  6922. [dic setValue:hide forKey:@"hide"];
  6923. NSString *sql = [NSString stringWithFormat:@"select c.item_id,c.item_count,m.stockUom,c.price,c.discount,m.name,m.description,c.line_note,m.availability from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id;",orderCode];
  6924. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6925. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6926. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  6927. // item id
  6928. int item_id = sqlite3_column_int(stmt, 0);
  6929. // count
  6930. int item_count = sqlite3_column_int(stmt, 1);
  6931. // stockUom
  6932. int stockUom = sqlite3_column_int(stmt, 2);
  6933. // unit price
  6934. NSString *str_price = [self textAtColumn:3 statement:stmt];
  6935. NSString* Price=nil;
  6936. if([str_price isEqualToString:@""])
  6937. {
  6938. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  6939. if(price==nil)
  6940. Price=@"No Price.";
  6941. else
  6942. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6943. }
  6944. else
  6945. {
  6946. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  6947. }
  6948. // discount
  6949. double discount = sqlite3_column_double(stmt, 4);
  6950. // name
  6951. NSString *name = [self textAtColumn:5 statement:stmt];
  6952. // description
  6953. NSString *description = [self textAtColumn:6 statement:stmt];
  6954. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  6955. // line note
  6956. NSString *line_note = [self textAtColumn:7 statement:stmt];
  6957. int avaulability = sqlite3_column_int(stmt, 8);
  6958. // img
  6959. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  6960. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false];
  6961. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6962. if(combine != nil)
  6963. {
  6964. // int citem=0;
  6965. int bcount=[[combine valueForKey:@"count"] intValue];
  6966. for(int bc=0;bc<bcount;bc++)
  6967. {
  6968. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6969. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6970. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6971. subTotal += uprice * modulus * item_count;
  6972. }
  6973. }
  6974. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  6975. [model_dic setValue:@"model" forKey:@"control"];
  6976. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  6977. [model_dic setValue:description forKey:@"description"];
  6978. [model_dic setValue:line_note forKey:@"note"];
  6979. [model_dic setValue:img forKey:@"img_url"];
  6980. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  6981. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  6982. [model_dic setValue:Price forKey:@"unit_price"];
  6983. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  6984. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  6985. if (combine) {
  6986. [model_dic setValue:combine forKey:@"combine"];
  6987. }
  6988. // well,what under the row is the info for total
  6989. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6990. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6991. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6992. int carton=[bsubtotaljson[@"carton"] intValue];
  6993. TotalCuft += cuft;
  6994. TotalWeight += weight;
  6995. TotalCarton += carton;
  6996. payments += subTotal;
  6997. //---------------------------
  6998. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  6999. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7000. }];
  7001. [dic setValue:@"Model Information" forKey:@"title"];
  7002. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7003. return dic;
  7004. }
  7005. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7006. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7007. [dic setValue:@"Remarks Content" forKey:@"title"];
  7008. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7009. // setting
  7010. NSDictionary *setting = params[@"setting"];
  7011. NSNumber *hide = setting[@"RemarksContentHide"];
  7012. [dic setValue:hide forKey:@"hide"];
  7013. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7014. orderCode = [self translateSingleQuote:orderCode];
  7015. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7016. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7017. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7018. int mustCall = sqlite3_column_int(stmt, 1);
  7019. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7020. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7021. NSDictionary *po_dic = @{
  7022. @"aname" : @"PO#",
  7023. @"control" : @"edit",
  7024. @"keyboard" : @"text",
  7025. @"name" : @"poNumber",
  7026. @"value" : poNumber
  7027. };
  7028. NSDictionary *must_call_dic = @{
  7029. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7030. @"control" : @"switch",
  7031. @"name" : @"must_call",
  7032. @"value" : mustCall ? @"true" : @"false"
  7033. };
  7034. NSDictionary *general_notes_dic = @{
  7035. @"aname" : @"General notes",
  7036. @"control" : @"text_view",
  7037. @"keyboard" : @"text",
  7038. @"name" : @"comments",
  7039. @"value" : generalNotes
  7040. };
  7041. NSDictionary *internal_notes_dic = @{
  7042. @"aname" : @"Internal notes",
  7043. @"control" : @"text_view",
  7044. @"keyboard" : @"text",
  7045. @"name" : @"internal_notes",
  7046. @"value" : internalNotes
  7047. };
  7048. [dic setValue:po_dic forKey:@"item_0"];
  7049. [dic setValue:must_call_dic forKey:@"item_1"];
  7050. [dic setValue:general_notes_dic forKey:@"item_2"];
  7051. [dic setValue:internal_notes_dic forKey:@"item_3"];
  7052. }];
  7053. return dic;
  7054. }
  7055. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7056. // params
  7057. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7058. orderCode = [self translateSingleQuote:orderCode];
  7059. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7060. // setting
  7061. NSDictionary *setting = params[@"setting"];
  7062. NSNumber *hide = setting[@"OrderTotalHide"];
  7063. [dic setValue:hide forKey:@"hide"];
  7064. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7065. __block double lift_gate_value = 0;
  7066. __block double handling_fee = 0;
  7067. __block double shipping = 0;
  7068. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7069. int lift_gate = sqlite3_column_int(stmt, 0);
  7070. lift_gate_value = sqlite3_column_double(stmt, 1);
  7071. shipping = sqlite3_column_double(stmt, 2);
  7072. handling_fee = sqlite3_column_double(stmt, 3);
  7073. if (!lift_gate) {
  7074. lift_gate_value = 0;
  7075. }
  7076. }];
  7077. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7078. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7079. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7080. double payments = [[total valueForKey:@"payments"] doubleValue];
  7081. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7082. double totalPrice = payments;
  7083. [dic setValue:@"Order Total" forKey:@"title"];
  7084. NSDictionary *payments_dic = @{
  7085. @"align" : @"right",
  7086. @"aname" : @"Payments/Credits",
  7087. @"control" : @"text",
  7088. @"name" : @"paymentsAndCredits",
  7089. @"type" : @"price",
  7090. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7091. };
  7092. [dic setValue:payments_dic forKey:@"item_0"];
  7093. NSDictionary *handling_fee_dic = @{
  7094. @"align" : @"right",
  7095. @"aname" : @"Handling Fee",
  7096. @"control" : @"text",
  7097. @"name" : @"handling_fee_value",
  7098. @"required" : @"true",
  7099. @"type" : @"price",
  7100. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7101. };
  7102. NSDictionary *shipping_dic = @{
  7103. @"align" : @"right",
  7104. @"aname" : @"Shipping*",
  7105. @"control" : @"text",
  7106. @"name" : @"shipping",
  7107. @"required" : @"true",
  7108. @"type" : @"price",
  7109. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7110. };
  7111. NSDictionary *lift_gate_dic = @{
  7112. @"align" : @"right",
  7113. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7114. @"control" : @"text",
  7115. @"name" : @"lift_gate_value",
  7116. @"required" : @"true",
  7117. @"type" : @"price",
  7118. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7119. };
  7120. int item_count = 1;
  7121. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7122. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7123. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7124. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7125. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7126. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7127. } else {
  7128. }
  7129. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7130. NSDictionary *total_price_dic = @{
  7131. @"align" : @"right",
  7132. @"aname" : @"Total",
  7133. @"control" : @"text",
  7134. @"name" : @"totalPrice",
  7135. @"type" : @"price",
  7136. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7137. };
  7138. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7139. NSDictionary *total_cuft_dic = @{
  7140. @"align" : @"right",
  7141. @"aname" : @"Total Cuft",
  7142. @"control" : @"text",
  7143. @"name" : @"",
  7144. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7145. };
  7146. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7147. NSDictionary *total_weight_dic = @{
  7148. @"align" : @"right",
  7149. @"aname" : @"Total Weight",
  7150. @"control" : @"text",
  7151. @"name" : @"",
  7152. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7153. };
  7154. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7155. NSDictionary *total_carton_dic = @{
  7156. @"align" : @"right",
  7157. @"aname" : @"Total Carton",
  7158. @"control" : @"text",
  7159. @"name" : @"",
  7160. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7161. };
  7162. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7163. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7164. return dic;
  7165. }
  7166. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7167. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7168. orderCode = [self translateSingleQuote:orderCode];
  7169. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7170. // setting
  7171. NSDictionary *setting = params[@"setting"];
  7172. NSNumber *hide = setting[@"SignatureHide"];
  7173. [dic setValue:hide forKey:@"hide"];
  7174. [dic setValue:@"Signature" forKey:@"title"];
  7175. __block NSString *pic_path = @"";
  7176. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7177. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7178. pic_path = [self textAtColumn:0 statement:stmt];
  7179. }];
  7180. NSDictionary *pic_dic = @{
  7181. @"aname" : @"Signature",
  7182. @"avalue" :pic_path,
  7183. @"control" : @"signature",
  7184. @"name" : @"sign_picpath",
  7185. @"value" : pic_path
  7186. };
  7187. [dic setValue:pic_dic forKey:@"item_0"];
  7188. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7189. return dic;
  7190. }
  7191. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7192. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7193. // setting
  7194. NSDictionary *setting = params[@"setting"];
  7195. NSNumber *hide = setting[@"ShippingMethodHide"];
  7196. [dic setValue:hide forKey:@"hide"];
  7197. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7198. orderCode = [self translateSingleQuote:orderCode];
  7199. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7200. __block NSString *logist = @"";
  7201. __block NSString *lift_gate = @"";
  7202. __block int lift_gate_integer = 0;
  7203. __block NSString *logistic_note = @"";
  7204. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7205. logist = [self textAtColumn:0 statement:stmt];
  7206. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7207. logistic_note = [self textAtColumn:2 statement:stmt];
  7208. }];
  7209. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7210. [dic setValue:@"Shipping Method" forKey:@"title"];
  7211. // val_0
  7212. int PERSONAL_PICK_UP_check = 0;
  7213. int USE_MY_CARRIER_check = 0;
  7214. NSString *logistic_note_text = @"";
  7215. int will_call_check = 0;
  7216. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7217. will_call_check = 1;
  7218. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7219. PERSONAL_PICK_UP_check = 1;
  7220. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7221. USE_MY_CARRIER_check = 1;
  7222. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7223. logistic_note = [logistic_note_array firstObject];
  7224. if (logistic_note_array.count > 1) {
  7225. logistic_note_text = [logistic_note_array lastObject];
  7226. }
  7227. }
  7228. }
  7229. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7230. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7231. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7232. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7233. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7234. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7235. NSDictionary *val0_subItem_item0 = @{
  7236. @"aname" : @"Option",
  7237. @"cadedate" : @{
  7238. @"count" : [NSNumber numberWithInteger:2],
  7239. @"val_0" : @{
  7240. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7241. @"refresh" : [NSNumber numberWithInteger:0],
  7242. @"value" : @"PERSONAL PICK UP",
  7243. @"value_id" : @"PERSONAL PICK UP"
  7244. },
  7245. @"val_1" : @{
  7246. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7247. @"refresh" : [NSNumber numberWithInteger:0],
  7248. @"sub_item" : @{
  7249. @"count" : [NSNumber numberWithInteger:1],
  7250. @"item_0" : @{
  7251. @"aname" : @"BOL",
  7252. @"control" : @"edit",
  7253. @"keyboard" : @"text",
  7254. @"name" : @"logist_note_text",
  7255. @"refresh" : [NSNumber numberWithInteger:0],
  7256. @"required" : @"false",
  7257. @"value" : logistic_note_text
  7258. }
  7259. },
  7260. @"value" : @"USE MY CARRIER",
  7261. @"value_id" : @"USE MY CARRIER"
  7262. }
  7263. },
  7264. @"control" : @"enum",
  7265. @"name" : @"logistic_note",
  7266. @"required" : @"true",
  7267. @"single_select" : @"true"
  7268. };
  7269. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  7270. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  7271. // val_1
  7272. lift_gate = [self valueInParams:params key:@"lift_gate"];
  7273. if([lift_gate isEqualToString:@""]) {
  7274. if (lift_gate_integer == 1) {
  7275. lift_gate = @"true";
  7276. } else {
  7277. lift_gate = @"false";
  7278. }
  7279. }
  7280. int common_carrier_check = 0;
  7281. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  7282. common_carrier_check = 1;
  7283. [params setValue:lift_gate forKey:@"lift_gate"];
  7284. }
  7285. NSDictionary *val_1 = @{
  7286. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7287. @"sub_item" : @{
  7288. @"count" : [NSNumber numberWithInteger:1],
  7289. @"item_0" : @{
  7290. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7291. @"control" : @"switch",
  7292. @"name" : @"lift_gate",
  7293. @"refresh" : [NSNumber numberWithInteger:1],
  7294. @"required" : @"true",
  7295. @"value" : lift_gate
  7296. }
  7297. },
  7298. @"value" : @"COMMON CARRIER",
  7299. @"value_id" : @"COMMON CARRIER"
  7300. };
  7301. // cadedate
  7302. NSDictionary *cadedate = @{
  7303. @"count" : [NSNumber numberWithInteger:2],
  7304. @"val_0" : val_0,
  7305. @"val_1" : val_1
  7306. };
  7307. // item_0
  7308. NSMutableDictionary *item_0 = @{
  7309. @"aname" : @"Shipping",
  7310. @"cadedate" : cadedate,
  7311. @"control" : @"enum",
  7312. @"name" : @"logist",
  7313. @"refresh" : [NSNumber numberWithInteger:1],
  7314. @"single_select" : @"true",
  7315. }.mutableCopy;
  7316. NSNumber *required = setting[@"ShippingMethodRequire"];
  7317. if ([required integerValue]) {
  7318. [item_0 setValue:@"true" forKey:@"required"];
  7319. }
  7320. [dic setValue:item_0 forKey:@"item_0"];
  7321. return dic;
  7322. }
  7323. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7324. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7325. orderCode = [self translateSingleQuote:orderCode];
  7326. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7327. __block int submit_as_integer = 0;
  7328. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7329. submit_as_integer = sqlite3_column_int(stmt, 0);
  7330. }];
  7331. int check11 = 0;
  7332. int check10 = 0;
  7333. if (submit_as_integer == 11) {
  7334. check11 = 1;
  7335. }
  7336. if (submit_as_integer == 10) {
  7337. check10 = 1;
  7338. }
  7339. // section_0
  7340. NSMutableDictionary *dic = @{
  7341. @"count" : @(1),
  7342. @"item_0" : @{
  7343. @"aname" : @"Submit Order As",
  7344. @"cadedate" : @{
  7345. @"count" : @(2),
  7346. @"val_0" : @{
  7347. @"check" : [NSNumber numberWithInteger:check11],
  7348. @"value" : @"Sales Order",
  7349. @"value_id" : @(11)
  7350. },
  7351. @"val_1" : @{
  7352. @"check" : [NSNumber numberWithInteger:check10],
  7353. @"value" : @"Quote",
  7354. @"value_id" : @(10)
  7355. }
  7356. },
  7357. @"control" : @"enum",
  7358. @"name" : @"erpOrderStatus",
  7359. @"required" : @"true",
  7360. @"single_select" : @"true"
  7361. },
  7362. @"title" : @"Order Type"
  7363. }.mutableCopy;
  7364. // setting
  7365. NSDictionary *setting = params[@"setting"];
  7366. NSNumber *hide = setting[@"OrderTypeHide"];
  7367. [dic setValue:hide forKey:@"hide"];
  7368. return dic;
  7369. }
  7370. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7371. countryCode = [self translateSingleQuote:countryCode];
  7372. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7373. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7374. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7375. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7376. if (name == NULL) {
  7377. name = "";
  7378. }
  7379. if (code == NULL) {
  7380. code = "";
  7381. }
  7382. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7383. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7384. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7385. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7386. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7387. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7388. }
  7389. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7390. [container setValue:stateDic forKey:key];
  7391. }] mutableCopy];
  7392. [ret removeObjectForKey:@"result"];
  7393. // failure 可以不用了,一样的
  7394. if (ret.allKeys.count == 0) {
  7395. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7396. [stateDic setValue:@"Other" forKey:@"value"];
  7397. [stateDic setValue:@"" forKey:@"value_id"];
  7398. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7399. if (state_code && [@"" isEqualToString:state_code]) {
  7400. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7401. }
  7402. NSString *key = [NSString stringWithFormat:@"val_0"];
  7403. [ret setValue:stateDic forKey:key];
  7404. }
  7405. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  7406. return ret;
  7407. }
  7408. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7409. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7410. orderCode = [self translateSingleQuote:orderCode];
  7411. NSString *sql = [NSString stringWithFormat:@"select paymentType,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,decrypt(credit_card_number),decrypt(credit_card_security_code),decrypt(credit_card_expiration_month),decrypt(credit_card_expiration_year),credit_card_city,credit_card_state from offline_order where so_id = '%@';",orderCode];
  7412. __block NSString *payType = @"";
  7413. __block NSString *firstName = @"";
  7414. __block NSString *lastName = @"";
  7415. __block NSString *addr1 = @"";
  7416. __block NSString *addr2 = @"";
  7417. __block NSString *zipcode = @"";
  7418. __block NSString *cardType = @"";
  7419. __block NSString *cardNumber = @"";
  7420. __block NSString *securityCode = @"";
  7421. __block NSString *month = @"";
  7422. __block NSString *year = @"";
  7423. __block NSString *city = @"";
  7424. __block NSString *state = @"";
  7425. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7426. payType = [self textAtColumn:0 statement:stmt];
  7427. firstName = [self textAtColumn:1 statement:stmt];
  7428. lastName = [self textAtColumn:2 statement:stmt];
  7429. addr1 = [self textAtColumn:3 statement:stmt];
  7430. addr2 = [self textAtColumn:4 statement:stmt];
  7431. zipcode = [self textAtColumn:5 statement:stmt];
  7432. cardType = [self textAtColumn:6 statement:stmt];
  7433. cardNumber = [self textAtColumn:7 statement:stmt];
  7434. securityCode = [self textAtColumn:8 statement:stmt];
  7435. month = [self textAtColumn:9 statement:stmt];
  7436. year = [self textAtColumn:10 statement:stmt];
  7437. city = [self textAtColumn:11 statement:stmt];
  7438. state = [self textAtColumn:12 statement:stmt];
  7439. }];
  7440. NSString *required = @"true";
  7441. // setting
  7442. NSDictionary *setting = params[@"setting"];
  7443. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7444. if ([requiredNumber integerValue]) {
  7445. required = @"true";
  7446. } else {
  7447. required = @"false";
  7448. }
  7449. // "section_2"
  7450. NSMutableDictionary *dic = @{
  7451. @"count" : @(1),
  7452. @"item_0" : @{
  7453. @"aname" : @"Payment",
  7454. @"required" : required,
  7455. @"cadedate" : @{
  7456. @"count" : @(6),
  7457. @"val_0" : @{
  7458. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  7459. @"value" : @"Check",
  7460. @"value_id" : @"Check"
  7461. },
  7462. @"val_1" : @{
  7463. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  7464. @"value" : @"Cash",
  7465. @"value_id" : @"Cash"
  7466. },
  7467. @"val_2" : @{
  7468. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  7469. @"value" : @"NET 30",
  7470. @"value_id" : @"NET 30"
  7471. },
  7472. @"val_3" : @{
  7473. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  7474. @"value" : @"Wire Transfer",
  7475. @"value_id" : @"Wire Transfer"
  7476. },
  7477. @"val_4" : @{
  7478. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  7479. @"sub_item" : @{
  7480. @"count" : @(3),
  7481. @"item_0" : @{
  7482. @"aname" : @"choose",
  7483. @"control" : @"multi_action",
  7484. @"count" : @(1),
  7485. @"item_0" : @{
  7486. @"aname" : @"Same as customer",
  7487. @"key_map" : @{
  7488. @"credit_card_address1" : @"customer_address1",
  7489. @"credit_card_address2" : @"customer_address2",
  7490. @"credit_card_city" : @"customer_city",
  7491. @"credit_card_first_name" : @"customer_first_name",
  7492. @"credit_card_last_name" : @"customer_last_name",
  7493. @"credit_card_state" : @"customer_state",
  7494. @"credit_card_zipcode" : @"customer_zipcode"
  7495. },
  7496. @"type" : @"pull"
  7497. }
  7498. },
  7499. @"item_1" : @{
  7500. @"aname" : @"",
  7501. @"color" : @"red",
  7502. @"control" : @"text",
  7503. @"name" : @"",
  7504. @"value" : @"USA Credit cards only"
  7505. },
  7506. @"item_2" : @{
  7507. @"aname" : @"Fill",
  7508. @"cadedate" : @{
  7509. @"count" : @(2),
  7510. @"val_0" : @{
  7511. @"check" : @(1),
  7512. @"sub_item" : @{
  7513. @"count" : @(11),
  7514. @"item_0" : @{
  7515. @"aname" : @"Type",
  7516. @"cadedate" : @{
  7517. @"count" : @(2),
  7518. @"val_0" : @{
  7519. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7520. @"value" : @"VISA",
  7521. @"value_id" : @(0)
  7522. },
  7523. @"val_1" : @{
  7524. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7525. @"value" : @"MASTER CARD",
  7526. @"value_id" : @(1)
  7527. }
  7528. },
  7529. @"control" : @"enum",
  7530. @"name" : @"credit_card_type",
  7531. @"required" : @"true",
  7532. @"single_select" : @"true"
  7533. },
  7534. @"item_1" : @{
  7535. @"aname" : @"Number",
  7536. @"control" : @"edit",
  7537. @"keyboard" : @"int",
  7538. @"length" : @"16",
  7539. @"name" : @"credit_card_number",
  7540. @"required" : @"true",
  7541. @"value" : cardNumber
  7542. },
  7543. @"item_10" : @{
  7544. @"aname" : @"State",
  7545. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7546. @"control" : @"enum",
  7547. @"enum" : @"true",
  7548. @"name" : @"credit_card_state",
  7549. @"required" : @"true",
  7550. @"single_select" : @"true"
  7551. },
  7552. @"item_2" : @{
  7553. @"aname" : @"Expiration Date",
  7554. @"control" : @"monthpicker",
  7555. @"name" : @"credit_card_expiration",
  7556. @"required" : @"true",
  7557. @"type" : @"date",
  7558. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  7559. },
  7560. @"item_3" : @{
  7561. @"aname" : @"Security Code",
  7562. @"control" : @"edit",
  7563. @"keyboard" : @"int",
  7564. @"length" : @"3",
  7565. @"name" : @"credit_card_security_code",
  7566. @"required" : @"true",
  7567. @"value" : securityCode
  7568. },
  7569. @"item_4" : @{
  7570. @"aname" : @"First Name",
  7571. @"control" : @"edit",
  7572. @"keyboard" : @"text",
  7573. @"name" : @"credit_card_first_name",
  7574. @"required" : @"true",
  7575. @"value" : firstName
  7576. },
  7577. @"item_5" : @{
  7578. @"aname" : @"Last Name",
  7579. @"control" : @"edit",
  7580. @"keyboard" : @"text",
  7581. @"name" : @"credit_card_last_name",
  7582. @"required" : @"true",
  7583. @"value" : lastName
  7584. },
  7585. @"item_6" : @{
  7586. @"aname" : @"Address 1",
  7587. @"control" : @"edit",
  7588. @"keyboard" : @"text",
  7589. @"name" : @"credit_card_address1",
  7590. @"required" : @"true",
  7591. @"value" : addr1
  7592. },
  7593. @"item_7" : @{
  7594. @"aname" : @"Address 2",
  7595. @"control" : @"edit",
  7596. @"keyboard" : @"text",
  7597. @"name" : @"credit_card_address2",
  7598. @"value" : addr2
  7599. },
  7600. @"item_8" : @{
  7601. @"aname" : @"zip code",
  7602. @"control" : @"edit",
  7603. @"keyboard" : @"text",
  7604. @"name" : @"credit_card_zipcode",
  7605. @"required" : @"true",
  7606. @"value" : zipcode
  7607. },
  7608. @"item_9" : @{
  7609. @"aname" : @"City",
  7610. @"control" : @"edit",
  7611. @"keyboard" : @"text",
  7612. @"name" : @"credit_card_city",
  7613. @"required" : @"true",
  7614. @"value" : city
  7615. }
  7616. },
  7617. @"value" : @"Fill Now",
  7618. @"value_id" : @""
  7619. },
  7620. @"val_1" : @{
  7621. @"check" : @(0),
  7622. @"value" : @"Fill Later",
  7623. @"value_id" : @""
  7624. }
  7625. },
  7626. @"control" : @"enum",
  7627. @"name" : @"",
  7628. @"single_select" : @"true"
  7629. }
  7630. },
  7631. @"value" : @"Credit Card",
  7632. @"value_id" : @"Credit Card"
  7633. },
  7634. @"val_5" : @{
  7635. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  7636. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  7637. @"value_id" : @"FOLLOW EXISTING"
  7638. }
  7639. },
  7640. @"control" : @"enum",
  7641. @"name" : @"paymentType",
  7642. @"single_select" : @"true"
  7643. },
  7644. @"title" : @"Payment Information"
  7645. }.mutableCopy;
  7646. NSNumber *hide = setting[@"PaymentInformationHide"];
  7647. [dic setValue:hide forKey:@"hide"];
  7648. return dic;
  7649. }
  7650. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  7651. {
  7652. sqlite3 *db = [iSalesDB get_db];
  7653. // UISetting
  7654. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7655. NSString *cachefolder = [paths objectAtIndex:0];
  7656. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  7657. NSData* json =nil;
  7658. json=[NSData dataWithContentsOfFile:img_cache];
  7659. NSError *error=nil;
  7660. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  7661. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  7662. [params setObject:setting forKey:@"setting"];
  7663. int section_count = 0;
  7664. // params
  7665. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7666. orderCode = [self translateSingleQuote:orderCode];
  7667. // 0 Order Type 1 Shipping Method 2 Payment Information
  7668. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  7669. // 0 Order Type
  7670. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  7671. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  7672. [ret setValue:order_type_dic forKey:key0];
  7673. // 1 Shipping Method
  7674. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  7675. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  7676. [ret setValue:shipping_method_dic forKey:key1];
  7677. // 2 Payment Information
  7678. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  7679. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  7680. [ret setValue:payment_info_dic forKey:key2];
  7681. // 3 Customer
  7682. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  7683. NSDictionary *customer_dic = [self customerDic:params db:db];
  7684. [ret setValue:customer_dic forKey:key3];
  7685. // 4 Ship To
  7686. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  7687. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  7688. [ret setValue:ship_to_dic forKey:key4];
  7689. // 5 Ship From
  7690. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  7691. if (![shipFromDisable integerValue]) {
  7692. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  7693. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  7694. [ret setValue:ship_from_dic forKey:key5];
  7695. }
  7696. // 6 Freight Bill To
  7697. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  7698. if (![freightBillToDisable integerValue]) {
  7699. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  7700. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  7701. [ret setValue:freight_bill_to forKey:key6];
  7702. }
  7703. // 7 Merchandise Bill To
  7704. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  7705. if (![merchandiseBillToDisable integerValue]) {
  7706. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  7707. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  7708. [ret setValue:merchandise_bill_to_dic forKey:key7];
  7709. }
  7710. // 8 Return To
  7711. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  7712. if (![returnToDisable integerValue]) {
  7713. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  7714. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  7715. [ret setValue:return_to_dic forKey:key8];
  7716. }
  7717. // 9 Model Information
  7718. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  7719. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  7720. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  7721. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  7722. [ret setValue:model_info_dic forKey:key9];
  7723. // 10 Remarks Content
  7724. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  7725. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  7726. [ret setValue:remarks_content_dic forKey:key10];
  7727. // 11 Order Total
  7728. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  7729. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  7730. [ret setValue:order_total_dic forKey:key11];
  7731. // 12 Signature
  7732. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  7733. NSDictionary *sign_dic = [self signatureDic:params db:db];
  7734. [ret setValue:sign_dic forKey:key12];
  7735. [ret setValue:@(section_count) forKey:@"section_count"];
  7736. [iSalesDB close_db:db];
  7737. return [RAUtils dict2data:ret];
  7738. // return nil;
  7739. }
  7740. #pragma mark addr editor
  7741. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  7742. NSMutableDictionary *new_item = [item mutableCopy];
  7743. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  7744. return new_item;
  7745. }
  7746. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  7747. {
  7748. // "is_subaction" = true;
  7749. // orderCode = MOB1608240002;
  7750. // password = 123456;
  7751. // "subaction_tag" = 1;
  7752. // user = EvanK;
  7753. // {
  7754. // "is_subaction" = true;
  7755. // orderCode = MOB1608240002;
  7756. // password = 123456;
  7757. // "refresh_trigger" = zipcode;
  7758. // "subaction_tag" = 1;
  7759. // user = EvanK;
  7760. // }
  7761. NSString *country_code = nil;
  7762. NSString *zipCode = nil;
  7763. NSString *stateCode = nil;
  7764. NSString *city = nil;
  7765. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  7766. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  7767. NSString *code_id = params[@"country"];
  7768. country_code = [self countryCodeByid:code_id];
  7769. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  7770. NSString *zip_code = params[@"zipcode"];
  7771. // 剔除全部为空格
  7772. int spaceCount = 0;
  7773. for (int i = 0; i < zip_code.length; i++) {
  7774. if ([zip_code characterAtIndex:i] == ' ') {
  7775. spaceCount++;
  7776. }
  7777. }
  7778. if (spaceCount == zip_code.length) {
  7779. zip_code = @"";
  7780. }
  7781. zipCode = zip_code;
  7782. if (zipCode.length > 0) {
  7783. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  7784. country_code = [dic valueForKey:@"country_code"];
  7785. if (!country_code) {
  7786. // country_code = @"US";
  7787. NSString *code_id = params[@"country"];
  7788. country_code = [self countryCodeByid:code_id];
  7789. }
  7790. stateCode = [dic valueForKey:@"state_code"];
  7791. if(!stateCode.length) {
  7792. stateCode = params[@"state"];
  7793. }
  7794. city = [dic valueForKey:@"city"];
  7795. if (!city.length) {
  7796. city = params[@"city"];
  7797. }
  7798. // zip code
  7799. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  7800. // [zipDic setValue:zipCode forKey:@"value"];
  7801. // [section_0 setValue:zipDic forKey:@"item_11"];
  7802. } else {
  7803. NSString *code_id = params[@"country"];
  7804. country_code = [self countryCodeByid:code_id];
  7805. stateCode = params[@"state"];
  7806. city = params[@"city"];
  7807. }
  7808. }
  7809. }
  7810. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  7811. // [ret removeObjectForKey:@"up_params"];
  7812. [ret setObject:@"New Address" forKey:@"title"];
  7813. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  7814. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  7815. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  7816. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  7817. // [country_dic removeObjectForKey:@"refresh"];
  7818. // [country_dic removeObjectForKey:@"restore"];
  7819. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  7820. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  7821. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  7822. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  7823. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  7824. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  7825. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  7826. // [zip_code_dic removeObjectForKey:@"refresh"];
  7827. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  7828. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  7829. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  7830. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  7831. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  7832. [cityDic setValue:city ? city : @"" forKey:@"value"];
  7833. [new_section_0 setObject:cityDic forKey:@"item_8"];
  7834. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  7835. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  7836. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  7837. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  7838. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  7839. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  7840. // country
  7841. NSString *countryCode = country_code == nil ? @"US" : country_code;
  7842. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  7843. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  7844. // state
  7845. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  7846. // NSDictionary *tmpDic = @{
  7847. // @"value" : @"Other",
  7848. // @"value_id" : @"",
  7849. // @"check" : [NSNumber numberWithInteger:0]
  7850. // };
  7851. //
  7852. // for (int i = 0; i < allState.allKeys.count; i++) {
  7853. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  7854. //
  7855. // NSDictionary *tmp = allState[key];
  7856. // [allState setValue:tmpDic forKey:key];
  7857. // tmpDic = tmp;
  7858. // }
  7859. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  7860. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  7861. [ret setValue:new_section_0 forKey:@"section_0"];
  7862. return [RAUtils dict2data:ret];
  7863. }
  7864. #pragma mark save addr
  7865. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  7866. {
  7867. // NSString *companyName = [self valueInParams:params key:@"company"];
  7868. // NSString *addr1 = [self valueInParams:params key:@"address"];
  7869. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  7870. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  7871. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  7872. // NSString *countryId = [self valueInParams:params key:@"country"];
  7873. // NSString *stateCode = [self valueInParams:params key:@"state"];
  7874. // NSString *city = [self valueInParams:params key:@"city"];
  7875. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  7876. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  7877. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  7878. // NSString *phone = [self valueInParams:params key:@"phone"];
  7879. // NSString *fax = [self valueInParams:params key:@"fax"];
  7880. // NSString *email = [self valueInParams:params key:@"email"];
  7881. return [self offline_saveContact:params update:NO isCustomer:NO];
  7882. }
  7883. #pragma mark cancel order
  7884. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  7885. {
  7886. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7887. NSString *order_id = [self valueInParams:params key:@"orderId"];
  7888. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  7889. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  7890. int ret = [iSalesDB execSql:sql];
  7891. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7892. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7893. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7894. // [dic setValue:@"160409" forKey:@"min_ver"];
  7895. return [RAUtils dict2data:dic];
  7896. }
  7897. #pragma mark sign order
  7898. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  7899. {
  7900. //参考 offline_saveBusinesscard
  7901. DebugLog(@"sign order params: %@",params);
  7902. // orderCode = MOB1608240002;
  7903. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  7904. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7905. orderCode = [self translateSingleQuote:orderCode];
  7906. NSString *picPath = [self valueInParams:params key:@"picpath"];
  7907. picPath = [self translateSingleQuote:picPath];
  7908. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  7909. int ret = [iSalesDB execSql:sql];
  7910. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7911. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7912. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7913. // [dic setValue:@"160409" forKey:@"min_ver"];
  7914. return [RAUtils dict2data:dic];
  7915. }
  7916. #pragma mark save order
  7917. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  7918. NSString *ret = [self valueInParams:params key:key];
  7919. if (translate) {
  7920. ret = [self translateSingleQuote:ret];
  7921. }
  7922. return ret;
  7923. }
  7924. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  7925. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  7926. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  7927. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  7928. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  7929. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  7930. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  7931. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  7932. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  7933. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  7934. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  7935. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  7936. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  7937. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  7938. if (![total_price isEqualToString:@""]) {
  7939. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  7940. } else {
  7941. total_price = @"";
  7942. }
  7943. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  7944. if ([paymentsAndCredits isEqualToString:@""]) {
  7945. paymentsAndCredits = @"";
  7946. } else {
  7947. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  7948. }
  7949. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  7950. if ([handling_fee_value isEqualToString:@""]) {
  7951. handling_fee_value = @"";
  7952. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  7953. handling_fee_value = @"";
  7954. } else {
  7955. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  7956. }
  7957. NSString *handling_fee_placeholder = handling_fee_value;
  7958. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  7959. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  7960. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  7961. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  7962. NSString *lift_gate_placeholder = @"";
  7963. if ([lift_gate_value isEqualToString:@""]) {
  7964. lift_gate_placeholder = @"";
  7965. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  7966. lift_gate_placeholder = @"";
  7967. } else {
  7968. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  7969. }
  7970. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  7971. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  7972. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  7973. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  7974. logistic_note = [logistic_note isEqualToString:@"USE MY CARRIER"] ? [NSString stringWithFormat:@"logistic_note = '%@\n\t%@',",logistic_note,logistic_note_text] : [NSString stringWithFormat:@"logistic_note = '%@',",logistic_note];
  7975. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  7976. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  7977. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  7978. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  7979. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  7980. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  7981. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  7982. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  7983. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  7984. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  7985. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  7986. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  7987. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  7988. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  7989. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  7990. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  7991. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  7992. BOOL number_nil = [credit_card_number isEqualToString:@""];
  7993. if (!number_nil) {
  7994. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  7995. }
  7996. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  7997. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  7998. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  7999. if (!security_code_nil) {
  8000. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  8001. }
  8002. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  8003. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  8004. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  8005. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  8006. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  8007. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  8008. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  8009. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  8010. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  8011. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  8012. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  8013. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  8014. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  8015. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  8016. NSString *contact_id = customer_cid;
  8017. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  8018. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  8019. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  8020. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  8021. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  8022. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  8023. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  8024. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  8025. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  8026. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  8027. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  8028. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  8029. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  8030. //
  8031. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  8032. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  8033. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  8034. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  8035. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  8036. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  8037. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  8038. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  8039. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  8040. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  8041. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  8042. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  8043. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  8044. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  8045. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  8046. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  8047. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  8048. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  8049. NSString *contactSql = [NSString stringWithFormat:@"update offline_contact set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ where contact_id = '%@'",contact_contact,contact_email,contact_phone,contact_fax,customer_name,customer_address1,customer_address2,customer_address3,customer_address4,customer_country,customer_state,customer_city,customer_zipcode,contact_id];
  8050. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  8051. if (receive_cid.length) {
  8052. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  8053. }
  8054. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  8055. if (receive_name.length) {
  8056. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  8057. }
  8058. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  8059. if (receive_ext.length) {
  8060. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  8061. }
  8062. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  8063. if (receive_contact.length) {
  8064. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  8065. }
  8066. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  8067. if (receive_phone.length) {
  8068. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  8069. }
  8070. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  8071. if (receive_email.length) {
  8072. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  8073. }
  8074. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  8075. if (receive_fax.length) {
  8076. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  8077. }
  8078. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  8079. if (sender_cid.length) {
  8080. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  8081. }
  8082. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  8083. if (sender_name.length) {
  8084. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  8085. }
  8086. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  8087. if (sender_ext.length) {
  8088. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  8089. }
  8090. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  8091. if(sender_contact.length) {
  8092. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  8093. }
  8094. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  8095. if (sender_phone.length) {
  8096. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  8097. }
  8098. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  8099. if (sender_fax.length) {
  8100. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  8101. }
  8102. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  8103. if (sender_email.length) {
  8104. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  8105. }
  8106. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  8107. if (shipping_billto_cid.length) {
  8108. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  8109. }
  8110. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  8111. if (shipping_billto_name.length) {
  8112. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  8113. }
  8114. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  8115. if (shipping_billto_ext.length) {
  8116. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  8117. }
  8118. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  8119. if (shipping_billto_contact.length) {
  8120. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  8121. }
  8122. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  8123. if (shipping_billto_phone.length) {
  8124. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  8125. }
  8126. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  8127. if (shipping_billto_fax.length) {
  8128. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  8129. }
  8130. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  8131. if (shipping_billto_email.length) {
  8132. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  8133. }
  8134. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  8135. if (billing_cid.length) {
  8136. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  8137. }
  8138. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  8139. if (billing_name.length) {
  8140. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  8141. }
  8142. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  8143. if (billing_ext.length) {
  8144. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  8145. }
  8146. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  8147. if (billing_contact.length) {
  8148. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  8149. }
  8150. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  8151. if (billing_phone.length) {
  8152. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  8153. }
  8154. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  8155. if (billing_fax.length) {
  8156. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  8157. }
  8158. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  8159. if (billing_email.length) {
  8160. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  8161. }
  8162. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  8163. if (returnto_cid.length) {
  8164. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  8165. }
  8166. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  8167. if (returnto_name.length) {
  8168. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  8169. }
  8170. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  8171. if (returnto_ext.length) {
  8172. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  8173. }
  8174. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  8175. if (returnto_contact.length) {
  8176. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  8177. }
  8178. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  8179. if (returnto_phone.length) {
  8180. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  8181. }
  8182. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  8183. if (returnto_fax.length) {
  8184. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  8185. }
  8186. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  8187. if (returnto_email.length) {
  8188. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  8189. }
  8190. NSString *order_status = @"status = 1,";
  8191. if (submit) {
  8192. order_status = @"status = -11,";
  8193. }
  8194. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  8195. NSString *sync_sql = @"";
  8196. if (submit) {
  8197. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  8198. [param setValue:sales_rep forKey:@"sales_rep"];
  8199. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  8200. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  8201. }
  8202. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@' %@ where so_id = '%@';",order_status,general_notes,internal_notes,poNumber,must_call,sign_picpath,total_price,paymentsAndCredits,handling_fee_placeholder,lift_gate,lift_gate_placeholder,logist,logistic_note,erpOrderStatus,paymentType,credit_card_address1,credit_card_address2,credit_card_city,credit_card_first_name,credit_card_last_name,credit_card_number,credit_card_security_code,credit_card_state,credit_card_type,credit_card_zipcode,credit_card_expiration_year,credit_card_expiration_month,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_email,receive_fax,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,so_id,sync_sql,so_id];
  8203. DebugLog(@"save order contactSql: %@",contactSql);
  8204. DebugLog(@"save order orderSql: %@",orderSql);
  8205. // int contact_ret = [iSalesDB execSql:contactSql];
  8206. int order_ret = [iSalesDB execSql:orderSql];
  8207. int ret = order_ret;
  8208. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8209. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8210. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8211. // [dic setValue:@"160409" forKey:@"min_ver"];
  8212. [dic setObject:so_id forKey:@"so#"];
  8213. return [RAUtils dict2data:dic];
  8214. }
  8215. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  8216. {
  8217. // id foo = nil;
  8218. // NSMutableArray *a = @[].mutableCopy;
  8219. // [a addObject:foo];
  8220. return [self saveorder:param submit:NO];
  8221. }
  8222. #pragma mark add to cart by name
  8223. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  8224. {
  8225. NSString *orderCode = [params objectForKey:@"orderCode"];
  8226. NSString *product_name = [params objectForKey:@"product_name"];
  8227. product_name = [self translateSingleQuote:product_name];
  8228. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8229. sqlite3 *db = [iSalesDB get_db];
  8230. __block NSMutableString *product_id_string = [NSMutableString string];
  8231. for (int i = 0; i < product_name_array.count; i++) {
  8232. NSString *name = [product_name_array objectAtIndex:i];
  8233. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  8234. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8235. int product_id = sqlite3_column_int(stmt, 0);
  8236. if (i == product_name_array.count - 1) {
  8237. [product_id_string appendFormat:@"%d",product_id];
  8238. } else {
  8239. [product_id_string appendFormat:@"%d,",product_id];
  8240. }
  8241. }];
  8242. }
  8243. NSDictionary *newParams = @{
  8244. @"product_id" : product_id_string,
  8245. @"orderCode" : orderCode
  8246. };
  8247. [iSalesDB close_db:db];
  8248. return [self offline_add2cart:[newParams mutableCopy]];
  8249. }
  8250. #pragma mark reset order
  8251. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  8252. {
  8253. UIApplication * app = [UIApplication sharedApplication];
  8254. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  8255. appDelegate.disable_trigger=true;
  8256. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  8257. appDelegate.disable_trigger=false;
  8258. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8259. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  8260. return [RAUtils dict2data:dic];
  8261. }
  8262. #pragma mark submit order
  8263. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  8264. {
  8265. return [self saveorder:params submit:YES];
  8266. }
  8267. #pragma mark copy order
  8268. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  8269. {
  8270. NSMutableDictionary *ret = @{}.mutableCopy;
  8271. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  8272. sqlite3 *db = [iSalesDB get_db];
  8273. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  8274. // 首先查看联系人是否active
  8275. NSString *customer_sql = [NSString stringWithFormat:@"select is_active from offline_contact where contact_id in (select customer_cid from offline_order where so_id = '%@');",order_code];
  8276. __block int customer_is_active = 1;
  8277. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8278. customer_is_active = sqlite3_column_int(stmt, 0);
  8279. }];
  8280. if (!customer_is_active) {
  8281. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8282. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8283. [iSalesDB close_db:db];
  8284. return [RAUtils dict2data:ret];
  8285. }
  8286. // new order
  8287. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  8288. NSString *new_order_code = [self get_offline_soid:db];
  8289. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  8290. user = [self translateSingleQuote:user];
  8291. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext,sales_terms) select '%@',1,submit_as,internal_notes,sales_rep,'%@',logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext,sales_terms from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  8292. __block int result = 1;
  8293. result = [iSalesDB execSql:insert_order_sql db:db];
  8294. if (!result) {
  8295. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8296. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8297. [iSalesDB close_db:db];
  8298. return [RAUtils dict2data:ret];
  8299. }
  8300. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  8301. // __block NSString *product_id = @"";
  8302. __weak typeof(self) weakSelf = self;
  8303. NSString *product_id_sql = [NSString stringWithFormat:@"select product_id,is_active from product WHERE product_id in (select product_id from offline_cart where so_no = '%@');",order_code];
  8304. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8305. int is_active = sqlite3_column_int(stmt, 1);
  8306. if (is_active) {
  8307. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  8308. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  8309. NSString *insert_cart_sql = [NSString stringWithFormat:@"insert into offline_cart (so_no,orderitem_id,product_id,item_id,item_count,bundle_item,type) select '%@',orderitem_id,product_id,item_id,item_count,bundle_item,type from offline_cart where product_id = %@ and so_no = '%@'",new_order_code,product_id,order_code];
  8310. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  8311. }
  8312. }];
  8313. // product_id = [product_id substringFromIndex:1];
  8314. //
  8315. // [self offline_add2cart:@{}.mutableCopy];
  8316. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8317. [iSalesDB close_db:db];
  8318. if (result) {
  8319. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8320. } else {
  8321. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8322. }
  8323. [ret setObject:new_order_code forKey:@"so_id"];
  8324. return [RAUtils dict2data:ret];
  8325. }
  8326. #pragma mark move wish list to cart
  8327. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  8328. NSString *collectId = params[@"collectId"];
  8329. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  8330. __block NSString *product_id = @"";
  8331. __block NSString *qty = @"";
  8332. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8333. int productId = sqlite3_column_int(stmt, 0);
  8334. int item_qty = sqlite3_column_int(stmt, 1);
  8335. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  8336. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  8337. }];
  8338. product_id = [product_id substringFromIndex:1];
  8339. qty = [qty substringFromIndex:1];
  8340. NSDictionary *newParams = @{
  8341. @"product_id" : product_id,
  8342. @"orderCode" : params[@"orderCode"],
  8343. @"qty" : qty
  8344. };
  8345. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  8346. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  8347. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  8348. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  8349. //
  8350. // int ret = [iSalesDB execSql:deleteSQL];
  8351. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  8352. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  8353. }
  8354. return [RAUtils dict2data:retDic];
  8355. }
  8356. #pragma mark - portfolio
  8357. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  8358. int sort = [[params valueForKey:@"sort"] integerValue];
  8359. int offset = [[params valueForKey:@"offset"] integerValue];
  8360. int limit = [[params valueForKey:@"limit"] integerValue];
  8361. NSString *orderBy = @"";
  8362. switch (sort) {
  8363. case 0:{
  8364. orderBy = @"modify_time desc";
  8365. }
  8366. break;
  8367. case 1:{
  8368. orderBy = @"modify_time asc";
  8369. }
  8370. break;
  8371. case 2:{
  8372. orderBy = @"name asc";
  8373. }
  8374. break;
  8375. case 3:{
  8376. orderBy = @"name desc";
  8377. }
  8378. break;
  8379. case 4:{
  8380. orderBy = @"description asc";
  8381. }
  8382. break;
  8383. default:
  8384. break;
  8385. }
  8386. NSString *sql = [NSString stringWithFormat:@"select p.product_id,p.name,p.description,ifnull(p.sheet_price,'null'),p.sheet_discount,ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.fashion_id,i.url,p.line_note,p.percent from offline_portfolio as p left join model_image as i on i.product_id = p.product_id where i.type = 1 and (p.is_delete is null or p.is_delete = 0) group by p.product_id order by %@ limit %d offset %d;",orderBy,limit,offset];
  8387. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8388. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8389. sqlite3 *db = [iSalesDB get_db];
  8390. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8391. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8392. int product_id = sqlite3_column_int(stmt, 0);
  8393. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8394. NSString *name = [self textAtColumn:1 statement:stmt];
  8395. NSString *description = [self textAtColumn:2 statement:stmt];
  8396. double price = sqlite3_column_double(stmt, 3);
  8397. double discount = sqlite3_column_double(stmt,4);
  8398. int qty = sqlite3_column_int(stmt, 5);
  8399. int percentage = sqlite3_column_int(stmt, 6);
  8400. int item_id = sqlite3_column_int(stmt, 7);
  8401. // int fashion_id = sqlite3_column_int(stmt, 8);
  8402. NSString *img_path = [self textAtColumn:9 statement:stmt];
  8403. NSString *line_note = [self textAtColumn:10 statement:stmt];
  8404. double percent = sqlite3_column_double(stmt, 11);
  8405. NSString *price_null = [self textAtColumn:3 statement:stmt];
  8406. if ([price_null isEqualToString:@"null"]) {
  8407. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8408. }
  8409. NSMutableDictionary *item = @{
  8410. @"linenotes": line_note,
  8411. @"check": @(1),
  8412. @"product_id": product_id_string,
  8413. @"available_qty": @(qty),
  8414. @"available_percent" : @(percent),
  8415. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  8416. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8417. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  8418. @"img": img_path,
  8419. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  8420. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  8421. }.mutableCopy;
  8422. if (percentage) {
  8423. [item removeObjectForKey:@"available_qty"];
  8424. } else {
  8425. [item removeObjectForKey:@"available_percent"];
  8426. }
  8427. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  8428. if ([qty_null isEqualToString:@"null"]) {
  8429. [item removeObjectForKey:@"available_qty"];
  8430. }
  8431. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8432. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8433. }];
  8434. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8435. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8436. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8437. }
  8438. [iSalesDB close_db:db];
  8439. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  8440. [dic setValue:@"" forKey:@"email_content"];
  8441. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  8442. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8443. return [RAUtils dict2data:dic];
  8444. }
  8445. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  8446. __block int qty = 0;
  8447. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8448. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8449. qty = sqlite3_column_int(stmt, 0);
  8450. }];
  8451. return qty;
  8452. }
  8453. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  8454. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  8455. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  8456. NSMutableDictionary * values = params[@"replaceValue"];
  8457. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  8458. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  8459. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  8460. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  8461. NSString *pdf_path = @"";
  8462. if (direct) {
  8463. configureParams = [self valueInParams:params key:@"pdfUrl"];
  8464. } else {
  8465. pdf_path = [self valueInParams:params key:@"pdfPath"];
  8466. }
  8467. NSString *create_user = [self valueInParams:params key:@"user"];
  8468. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  8469. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  8470. // model info
  8471. NSString *sql = [NSString stringWithFormat:@"select product_id,ifnull(sheet_price,'null'),ifnull(sheet_discount,'null'),ifnull(available_qty,'null'),percentage,item_id,line_note,percent from offline_portfolio where product_id in (%@);",product_ids];
  8472. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8473. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  8474. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8475. sqlite3 *db = [iSalesDB get_db];
  8476. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8477. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8478. int product_id = sqlite3_column_int(stmt, 0);
  8479. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8480. double price = sqlite3_column_double(stmt, 1);
  8481. double discount = sqlite3_column_double(stmt,2);
  8482. int qty = sqlite3_column_int(stmt, 3);
  8483. int percentage = sqlite3_column_int(stmt, 4);
  8484. int item_id = sqlite3_column_int(stmt, 5);
  8485. NSString *line_note = [self textAtColumn:6 statement:stmt];
  8486. double percent = sqlite3_column_double(stmt, 7);
  8487. NSString *price_null = [self textAtColumn:1 statement:stmt];
  8488. /* if ([price_null isEqualToString:@"null"]) {
  8489. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8490. }
  8491. */
  8492. [product_ids_string appendFormat:@"%@,",product_id_string];
  8493. // Regular Price
  8494. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  8495. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  8496. // QTY
  8497. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  8498. if ([qty_null isEqualToString:@"null"]) {
  8499. // 查available
  8500. qty = [self model_QTY:product_id_string db:db];
  8501. }
  8502. if (percentage) {
  8503. qty = qty * percent / 100;
  8504. }
  8505. // Special Price
  8506. if ([price_null isEqualToString:@"null"]) {
  8507. // price = regular price
  8508. price = [regular_price_str doubleValue];
  8509. }
  8510. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  8511. if (![discount_null isEqualToString:@"null"]) {
  8512. price = price * (1 - discount / 100.0);
  8513. }
  8514. NSMutableDictionary *item = @{
  8515. @"line_note": line_note,
  8516. @"product_id": product_id_string,
  8517. @"available_qty": @(qty),
  8518. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8519. @"regular_price" : regular_price_str,
  8520. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  8521. }.mutableCopy;
  8522. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8523. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8524. }];
  8525. [iSalesDB close_db:db];
  8526. if (product_ids_string.length > 0) {
  8527. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  8528. }
  8529. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  8530. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  8531. return [RAUtils dict2data:resultDictionary];
  8532. }
  8533. NSString *model_info = [RAUtils dict2string:dic];
  8534. // 创建PDF
  8535. // 在preview情况下保存,则不需要新建了
  8536. if (direct) {
  8537. NSMutableDictionary *tear_sheet_params = params;
  8538. // if (tear_sheet_id) {
  8539. // tear_sheet_params = values;
  8540. // }
  8541. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  8542. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  8543. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  8544. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  8545. resultDictionary = pdfInfo.mutableCopy;
  8546. } else { // 创建PDF失败
  8547. return pdfData;
  8548. }
  8549. } else {
  8550. // pdf_path 就是本地路径
  8551. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  8552. }
  8553. // 将文件移动到PDF Cache文件夹
  8554. NSString *newPath = [pdf_path lastPathComponent];
  8555. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8556. NSString *cachefolder = [paths objectAtIndex:0];
  8557. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8558. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  8559. NSFileManager *fileManager = [NSFileManager defaultManager];
  8560. NSError *error = nil;
  8561. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  8562. if (error) { // 移动文件失败
  8563. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8564. return [RAUtils dict2data:resultDictionary];
  8565. }
  8566. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  8567. pdf_path = [newPath lastPathComponent];
  8568. // 保存信息
  8569. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  8570. NSString *off_params = [RAUtils dict2string:params];
  8571. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  8572. pdf_path = [self translateSingleQuote:pdf_path];
  8573. create_user = [self translateSingleQuote:create_user];
  8574. tear_note = [self translateSingleQuote:tear_note];
  8575. tear_name = [self translateSingleQuote:tear_name];
  8576. model_info = [self translateSingleQuote:model_info];
  8577. configureParams = [self translateSingleQuote:configureParams];
  8578. off_params = [self translateSingleQuote:off_params];
  8579. NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (pdf_path,create_user,tear_note,tear_name,model_info,configureParams,is_local,off_params) values ('%@','%@','%@','%@','%@','%@',1,'%@');",pdf_path,create_user,tear_note,tear_name,model_info,configureParams,off_params];
  8580. if (tear_sheet_id) {
  8581. int _id = [tear_sheet_id integerValue];
  8582. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  8583. }
  8584. int result = [iSalesDB execSql:save_pdf_sql];
  8585. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8586. //
  8587. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  8588. if (remove_Item) {
  8589. // portfolioId
  8590. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  8591. }
  8592. return [RAUtils dict2data:resultDictionary];
  8593. }
  8594. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  8595. return [self offline_savePDF:params direct:YES];
  8596. }
  8597. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  8598. return [self offline_savePDF:params direct:NO];
  8599. }
  8600. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  8601. int offset = [[params valueForKey:@"offset"] integerValue];
  8602. int limit = [[params valueForKey:@"limit"] integerValue];
  8603. NSString *keyword = [params valueForKey:@"keyWord"];
  8604. NSString *where = @"where is_delete is null or is_delete = 0";
  8605. if (keyword.length) {
  8606. keyword = [self translateSingleQuote:keyword];
  8607. where = [NSString stringWithFormat:@"where (is_delete is null or is_delete = 0 ) and lower(create_user) like '%%%@%%' or lower(tear_note) like '%%%@%%' or lower(tear_name) like '%%%@%%'",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString];
  8608. }
  8609. NSString *sql = [NSString stringWithFormat:@"select tear_name,tear_note,createtime,create_user,pdf_path,_id,model_info,off_params from offline_pdf %@ order by createtime desc limit %d offset %d;",where,limit,offset];// tearsheets_id ---> _id
  8610. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8611. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8612. NSString *cachefolder = [paths objectAtIndex:0];
  8613. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8614. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8615. NSString *name = [self textAtColumn:0 statement:stmt];
  8616. NSString *note = [self textAtColumn:1 statement:stmt];
  8617. NSString *time = [self textAtColumn:2 statement:stmt];
  8618. NSString *user = [self textAtColumn:3 statement:stmt];
  8619. NSString *path = [self textAtColumn:4 statement:stmt];
  8620. NSString *model_info = [self textAtColumn:6 statement:stmt];
  8621. NSString *off_params = [self textAtColumn:7 statement:stmt];
  8622. path = [pdfFolder stringByAppendingPathComponent:path];
  8623. BOOL bdir=NO;
  8624. NSFileManager* fileManager = [NSFileManager defaultManager];
  8625. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  8626. {
  8627. //pdf文件不存在
  8628. path=nil;
  8629. }
  8630. time = [self changeDateTimeFormate:time];
  8631. time = [time stringByAppendingString:@" PST"];
  8632. int sheet_id = sqlite3_column_int(stmt, 5);
  8633. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  8634. item[@"tearsheetsId"]=@(sheet_id);
  8635. item[@"pdf_path"]=path;
  8636. item[@"create_time"]=time;
  8637. item[@"create_user"]=user;
  8638. item[@"tear_note"]=note;
  8639. item[@"tear_name"]=name;
  8640. item[@"isLocalFile"]=@"true";
  8641. item[@"model_info"]=model_info;
  8642. item[@"off_params"]=off_params;
  8643. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8644. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8645. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8646. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8647. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8648. }];
  8649. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8650. return [RAUtils dict2data:dic];
  8651. }
  8652. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  8653. NSString *prodct_ids = [params objectForKey:@"product_id"];
  8654. // NSString *user = [params objectForKey:@"user"];
  8655. // NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and (select count(0) from offline_portfolio where product_id in (%@) and (is_delete is null or is_delete = 0)) = 0);",prodct_ids,prodct_ids];
  8656. NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and p.product_id not in (select product_id from offline_portfolio));insert into offline_portfolio (product_id,name,description,item_id,is_delete) select * from (select p.product_id,p.name,p.description,p.item_id,0 from product as p left join offline_portfolio as f on p.product_id = f.product_id where p.product_id in (%@) and p.product_id in (select product_id from offline_portfolio) and f.is_delete = 1);delete from offline_portfolio where (select count(0) from offline_portfolio where product_id in (%@)) > 1 and is_delete = 1;",prodct_ids,prodct_ids,prodct_ids];
  8657. int result = [iSalesDB execSql:sql];
  8658. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8659. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  8660. if (result == RESULT_TRUE) {
  8661. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  8662. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  8663. sqlite3 *db = [iSalesDB get_db];
  8664. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  8665. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  8666. [iSalesDB close_db:db];
  8667. }
  8668. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8669. return [RAUtils dict2data:dic];
  8670. }
  8671. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  8672. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  8673. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  8674. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  8675. // Regurlar Price
  8676. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  8677. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  8678. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8679. __block long val_count = 0;
  8680. NSString *sql = @"select name,type,order_by from price order by order_by";
  8681. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8682. NSString *name = [self textAtColumn:0 statement:stmt];
  8683. int type = sqlite3_column_int(stmt, 1);
  8684. int order_by = sqlite3_column_int(stmt, 2);
  8685. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8686. NSDictionary *price_dic = @{
  8687. @"value" : name,
  8688. @"value_id" : [NSNumber numberWithInteger:type],
  8689. @"check" : order_by == 0 ? @(1) : @(0)
  8690. };
  8691. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8692. val_count = ++(*count);
  8693. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8694. val_count = 0;
  8695. }];
  8696. [cadedate setObject:@{@"value" : @"None",
  8697. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8698. val_count++;
  8699. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  8700. [price setObject:cadedate forKey:@"cadedate"];
  8701. [section1 setObject:price forKey:@"item_2"];
  8702. [dic setObject:section1 forKey:@"section_1"];
  8703. return [RAUtils dict2data:dic];
  8704. }
  8705. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  8706. NSString *product_id = [params objectForKey:@"fashionId"];
  8707. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8708. __block int result = RESULT_TRUE;
  8709. __block int qty = 0;
  8710. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8711. qty = sqlite3_column_int(stmt, 0);
  8712. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8713. result = RESULT_FALSE;
  8714. }];
  8715. NSMutableDictionary *dic = @{
  8716. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  8717. @"mode" : @"Regular Mode",
  8718. @"quantity_available" : @(qty),
  8719. @"result" : @(result),
  8720. }.mutableCopy;
  8721. return [RAUtils dict2data:dic];
  8722. }
  8723. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  8724. NSString *item_ids = [params objectForKey:@"item_id"];
  8725. NSString *line_notes = [params objectForKey:@"notes"];
  8726. NSString *price_str = [params objectForKey:@"price"];
  8727. NSString *discount_str = [params objectForKey:@"discount"];
  8728. NSString *percent = [params objectForKey:@"available_percent"];
  8729. NSString *qty = [params objectForKey:@"available_qty"];
  8730. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  8731. int dot = 0;
  8732. if (line_notes) {
  8733. line_notes = [self translateSingleQuote:line_notes];
  8734. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  8735. sql = [sql stringByAppendingString:line_notes];
  8736. dot = 1;
  8737. } else {
  8738. line_notes = @"";
  8739. }
  8740. if (price_str) {
  8741. if (dot) {
  8742. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  8743. } else {
  8744. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  8745. dot = 1;
  8746. }
  8747. sql = [sql stringByAppendingString:price_str];
  8748. } else {
  8749. price_str = @"";
  8750. }
  8751. if (discount_str) {
  8752. if (dot) {
  8753. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  8754. } else {
  8755. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  8756. dot = 1;
  8757. }
  8758. sql = [sql stringByAppendingString:discount_str];
  8759. } else {
  8760. discount_str = @"";
  8761. }
  8762. if (percent) {
  8763. if (dot) {
  8764. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  8765. } else {
  8766. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  8767. dot = 1;
  8768. }
  8769. sql = [sql stringByAppendingString:percent];
  8770. } else {
  8771. percent = @"";
  8772. }
  8773. if (qty) {
  8774. if (dot) {
  8775. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  8776. } else {
  8777. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  8778. dot = 1;
  8779. }
  8780. sql = [sql stringByAppendingString:qty];
  8781. } else {
  8782. qty = @"";
  8783. }
  8784. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  8785. sql = [sql stringByAppendingString:where];
  8786. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8787. int result = [iSalesDB execSql:sql];
  8788. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8789. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8790. return [RAUtils dict2data:dic];
  8791. }
  8792. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  8793. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  8794. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8795. sqlite3 *db = [iSalesDB get_db];
  8796. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set is_delete = 1 where item_id in (%@) and (is_delete is null or is_delete = 0);",portfolio_ids];
  8797. int result = [iSalesDB execSql:sql db:db];
  8798. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8799. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8800. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  8801. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  8802. [iSalesDB close_db:db];
  8803. return [RAUtils dict2data:dic];
  8804. }
  8805. + (void)offline_removePDFWithName:(NSString *)name {
  8806. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8807. NSString *cachefolder = [paths objectAtIndex:0];
  8808. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8809. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  8810. NSFileManager *fileManager = [NSFileManager defaultManager];
  8811. [fileManager removeItemAtPath:path error:nil];
  8812. }
  8813. + (void)offline_clear_PDFCache {
  8814. NSFileManager *fileManager = [NSFileManager defaultManager];
  8815. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8816. NSString *cachefolder = [paths objectAtIndex:0];
  8817. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8818. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  8819. for (NSString *path in pdf_files) {
  8820. [self offline_removePDFWithName:[path lastPathComponent]];
  8821. }
  8822. }
  8823. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  8824. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8825. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  8826. NSString *user = [params objectForKey:@"user"];
  8827. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  8828. if (![create_user isEqualToString:user]) {
  8829. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8830. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8831. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  8832. return [RAUtils dict2data:dic];
  8833. }
  8834. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8835. __block int is_local = 0;
  8836. __block NSString *path = @"";
  8837. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8838. is_local = sqlite3_column_int(stmt, 0);
  8839. path = [self textAtColumn:1 statement:stmt];
  8840. }];
  8841. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8842. if (is_local == 1) {
  8843. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8844. // 删除文件
  8845. [self offline_removePDFWithName:path];
  8846. }
  8847. int result = [iSalesDB execSql:sql];
  8848. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8849. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8850. return [RAUtils dict2data:dic];
  8851. }
  8852. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  8853. {
  8854. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  8855. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  8856. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  8857. NSString* where=@"1=1";
  8858. if (ver!=nil) {
  8859. where=@"is_dirty=1";
  8860. }
  8861. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,name,description,item_id,fashion_id,ifnull(available_qty,'null'),ifnull(percentage,'null'),ifnull(percent,'null'),ifnull(sheet_price,'null'),sheet_discount,img,line_note,is_delete,createtime,modify_time from offline_portfolio where %@",where];
  8862. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  8863. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8864. sqlite3 *db = [iSalesDB get_db];
  8865. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8866. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  8867. NSInteger _id = sqlite3_column_int(stmt, 0);
  8868. NSInteger product_id = sqlite3_column_int(stmt, 1);
  8869. NSString *name = [self textAtColumn:2 statement:stmt];
  8870. NSString *desc = [self textAtColumn:3 statement:stmt];
  8871. NSInteger item_id = sqlite3_column_int(stmt, 4);
  8872. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  8873. NSInteger qty = sqlite3_column_int(stmt, 6);
  8874. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  8875. double percent = sqlite3_column_double(stmt, 8);
  8876. double price = sqlite3_column_double(stmt, 9);
  8877. double discount = sqlite3_column_double(stmt, 10);
  8878. NSString *img = [self textAtColumn:11 statement:stmt];
  8879. NSString *line_note = [self textAtColumn:12 statement:stmt];
  8880. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  8881. NSString *create_time = [self textAtColumn:14 statement:stmt];
  8882. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  8883. NSString *price_null = [self textAtColumn:9 statement:stmt];
  8884. if ([price_null isEqualToString:@"null"]) {
  8885. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8886. }
  8887. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  8888. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  8889. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  8890. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  8891. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  8892. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  8893. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  8894. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  8895. if ([qty_null isEqualToString:@"null"]) {
  8896. // [item setValue:@"null" forKey:@"available_qty"];
  8897. } else {
  8898. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  8899. }
  8900. if ([is_percent_null isEqualToString:@"null"]) {
  8901. // [item setValue:@"null" forKey:@"percentage"];
  8902. } else {
  8903. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  8904. }
  8905. if ([percent_null isEqualToString:@"null"]) {
  8906. // [item setValue:@"null" forKey:@"percent"];
  8907. } else {
  8908. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  8909. }
  8910. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  8911. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  8912. [item setValue:name forKey:@"name"];
  8913. [item setValue:desc forKey:@"description"];
  8914. [item setValue:img forKey:@"img"];
  8915. [item setValue:line_note forKey:@"line_note"];
  8916. [item setValue:create_time forKey:@"createtime"];
  8917. [item setValue:modify_time forKey:@"modifytime"];
  8918. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8919. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8920. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  8921. }];
  8922. [iSalesDB close_db:db];
  8923. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8924. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8925. }
  8926. return ret;
  8927. }
  8928. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  8929. {
  8930. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  8931. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  8932. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  8933. NSString* where=@"1=1";
  8934. if (ver!=nil) {
  8935. where=@"is_dirty=1";
  8936. }
  8937. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,ifnull(tearsheets_id,'null'),pdf_path,create_user,tear_note,tear_name,model_info,createtime,modify_time,configureParams,off_params,is_delete,is_local from offline_pdf where %@",where];
  8938. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  8939. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8940. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  8941. NSInteger _id = sqlite3_column_int(stmt, 0);
  8942. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  8943. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  8944. NSString *create_user = [self textAtColumn:3 statement:stmt];
  8945. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  8946. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  8947. NSString *model_info = [self textAtColumn:6 statement:stmt];
  8948. NSString *createtime = [self textAtColumn:7 statement:stmt];
  8949. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  8950. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  8951. NSString *off_params = [self textAtColumn:10 statement:stmt];
  8952. NSString *uuid = [NSUUID UUID].UUIDString;
  8953. int is_delete = sqlite3_column_int(stmt, 11);
  8954. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  8955. int is_local = sqlite3_column_int(stmt, 12);
  8956. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  8957. if (![tearsheet_id_null isEqualToString:@"null"]) {
  8958. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  8959. }
  8960. [item setObject:pdf_path forKey:@"pdf_path"];
  8961. [item setObject:create_user forKey:@"create_user"];
  8962. [item setObject:tear_note forKey:@"tear_note"];
  8963. [item setObject:tear_name forKey:@"tear_name"];
  8964. [item setObject:model_info forKey:@"model_info"];
  8965. [item setObject:createtime forKey:@"createtime"];
  8966. [item setObject:modifytime forKey:@"modifytime"];
  8967. [item setObject:urlParams forKey:@"urlParams"];
  8968. [item setObject:off_params forKey:@"off_params"];
  8969. [item setObject:uuid forKey:@"pdf_token"];
  8970. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  8971. NSDictionary *off_Params_dic = [RAUtils string2dict:off_params];
  8972. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  8973. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  8974. } else {
  8975. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8976. }
  8977. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8978. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  8979. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8980. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8981. }];
  8982. return ret;
  8983. }
  8984. @end