OLDataProvider.m 444 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805
  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. @interface OLDataProvider ()
  21. @end
  22. @implementation OLDataProvider
  23. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  24. {
  25. 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];
  26. NSString* ret=@"No Price.";
  27. sqlite3_stmt * statement;
  28. // int count=0;
  29. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  30. {
  31. if (sqlite3_step(statement) == SQLITE_ROW)
  32. {
  33. char *price = (char*)sqlite3_column_text(statement, 0);
  34. if(price==nil)
  35. price="";
  36. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  37. double dp= [nsprice doubleValue];
  38. ret=[NSString stringWithFormat:@"%.2f",dp];
  39. }
  40. sqlite3_finalize(statement);
  41. }
  42. return ret;
  43. // // [iSalesDB close_db:db];
  44. //
  45. // return nil;
  46. //// if(dprice==DBL_MAX)
  47. //// ret= nil;
  48. //// else
  49. //// ret= [NSNumber numberWithDouble:dprice];
  50. //// return ret;
  51. }
  52. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  53. {
  54. NSString* ret= nil;
  55. NSString *sqlQuery = nil;
  56. // 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
  57. 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];
  58. sqlite3_stmt * statement;
  59. // int count=0;
  60. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  61. {
  62. if (sqlite3_step(statement) == SQLITE_ROW)
  63. {
  64. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  65. if(imgurl==nil)
  66. imgurl="";
  67. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  68. ret=nsimgurl;
  69. }
  70. sqlite3_finalize(statement);
  71. }
  72. else
  73. {
  74. ret=@"";
  75. }
  76. // [iSalesDB close_db:db];
  77. // DebugLog(@"data string: %@",ret );
  78. return ret;
  79. }
  80. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  81. {
  82. //生成portfolio pdf需要的数据
  83. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  84. data[@"npd_url"]=@"www.newpacificdirect.com";
  85. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  86. [formatter setDateFormat:@"MMMM yyyy"];
  87. NSString* date = [formatter stringFromDate:[NSDate date]];
  88. data[@"create_month"]=date;
  89. data[@"company_name"]=@"New Pacific Direct,Inc.";
  90. data[@"catalog_name"]=params[@"catalog_name"];
  91. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  92. data[@"TOTAL_PAGE"]=0;
  93. data[@"CURRENT_PAGE"]=0;
  94. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  95. bool show_stockout=[params[@"show_stockout"] boolValue];
  96. NSString *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",params[@"product_ids"],params[@"product_ids"]];
  97. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  98. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  99. sqlite3 *db = [iSalesDB get_db];
  100. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  101. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  102. int product_id = sqlite3_column_int(stmt, 0);
  103. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  104. NSString *name = [self textAtColumn:1 statement:stmt];
  105. NSString *description = [self textAtColumn:2 statement:stmt];
  106. double price = sqlite3_column_double(stmt, 3);
  107. double discount = sqlite3_column_double(stmt,4);
  108. int qty = sqlite3_column_int(stmt, 5);
  109. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  110. int is_percentage = sqlite3_column_int(stmt, 6);
  111. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  112. int item_id = sqlite3_column_int(stmt, 7);
  113. // int fashion_id = sqlite3_column_int(stmt, 8);
  114. NSString *line_note = [self textAtColumn:9 statement:stmt];
  115. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  116. double percent = sqlite3_column_double(stmt, 10);
  117. // int portfolio_id = sqlite3_column_int(stmt, 11);
  118. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  119. int availability = sqlite3_column_int(stmt, 12);
  120. NSString *color = [self textAtColumn:13 statement:stmt];
  121. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  122. NSString *demension = [self textAtColumn:15 statement:stmt];
  123. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  124. NSString *material = [self textAtColumn:17 statement:stmt];
  125. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  126. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  127. double volume = sqlite3_column_double(stmt, 19);
  128. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  129. double weight = sqlite3_column_double(stmt, 20);
  130. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  131. int model_set = sqlite3_column_int(stmt, 21);
  132. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  133. double load_ability = sqlite3_column_double(stmt, 22);
  134. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  135. NSString *assembling = [self textAtColumn:24 statement:stmt];
  136. NSString *made_in = [self textAtColumn:25 statement:stmt];
  137. if(is_percentage==0&&qty==0&&!show_stockout)
  138. {
  139. return;
  140. }
  141. if((is_percentage!=0||s_is_percentage.length==0)&&availability==0&&!show_stockout)
  142. {
  143. return;
  144. }
  145. if(is_percentage)
  146. {
  147. qty=availability;
  148. }
  149. else
  150. {
  151. percent=1;
  152. }
  153. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  154. // if ([qty_null isEqualToString:@"null"]) {
  155. // qty=availability;
  156. // }
  157. //
  158. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  159. // if ([qty_percent_null isEqualToString:@"null"]) {
  160. // percentage=1;
  161. // }
  162. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)(qty*percent+0.5)];
  163. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  164. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  165. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  166. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  167. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  168. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  169. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  170. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  171. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  172. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  173. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  174. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  175. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  176. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  177. if([params[@"availability"] boolValue]==false || (s_qty.length==0 && s_availability.length==0))
  178. str_availability=@"";
  179. if([params[@"color"] boolValue]==false || color.length==0 )
  180. str_color=@"";
  181. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  182. str_model_set=@"";
  183. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  184. str_legcolor=@"";
  185. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  186. str_demension=@"";
  187. if([params[@"seat_height"] boolValue]==false && seat_height.length==0 )
  188. str_seat_height=@"";
  189. if([params[@"material"] boolValue]==false && material.length==0 )
  190. str_meterial=@"";
  191. if([params[@"box_dimension"] boolValue]==false && box_dim.length==0 )
  192. str_box_dim=@"";
  193. if([params[@"volume"] boolValue]==false && s_volume.length==0 )
  194. str_volume=@"";
  195. if([params[@"weight"] boolValue]==false && s_weight.length==0 )
  196. str_weight=@"";
  197. if([params[@"load_ability"] boolValue]==false && s_load_ability.length==0 )
  198. str_load_ability=@"";
  199. if([params[@"fabric_content"] boolValue]==false && fabric_content.length==0 )
  200. str_fabric_content=@"";
  201. if([params[@"assembling"] boolValue]==false && assembling.length==0 )
  202. str_assembling=@"";
  203. if([params[@"made"] boolValue]==false && made_in.length==0 )
  204. str_made_in=@"";
  205. if([params[@"show_line_note"] boolValue]==false && line_note.length==0 )
  206. str_line_note=@"";
  207. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  208. [arr_detail addObject:str_color];
  209. [arr_detail addObject:str_model_set];
  210. [arr_detail addObject:str_legcolor];
  211. [arr_detail addObject:str_demension];
  212. [arr_detail addObject:str_seat_height];
  213. [arr_detail addObject:str_meterial];
  214. [arr_detail addObject:str_box_dim];
  215. [arr_detail addObject:str_volume];
  216. [arr_detail addObject:str_weight];
  217. [arr_detail addObject:str_load_ability];
  218. [arr_detail addObject:str_fabric_content];
  219. [arr_detail addObject:str_assembling];
  220. [arr_detail addObject:str_made_in];
  221. [arr_detail addObject:str_line_note];
  222. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  223. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  224. //model image;
  225. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  226. // qr image
  227. NSString* qrpath=nil;
  228. if([params[@"show_barcode"] boolValue])
  229. {
  230. NSString* temp = NSTemporaryDirectory();
  231. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  232. qrpath=[temp stringByAppendingPathComponent:filename];
  233. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  234. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  235. }
  236. NSString* set_price=@"";
  237. NSString *price_null = [self textAtColumn:3 statement:stmt];
  238. if ([price_null isEqualToString:@"null"]) {
  239. }
  240. else
  241. {
  242. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount)];
  243. }
  244. NSString* get_price=@"";
  245. {
  246. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  247. NSString* price = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  248. // DebugLog(@"price time interval");
  249. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  250. if(price==nil)
  251. get_price=@"Price:No Price.";
  252. else
  253. get_price=[NSString stringWithFormat:@"Price: $ %@",price];
  254. }
  255. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  256. item[@"img"]=img_path;
  257. item[@"img_qr"]=qrpath;
  258. item[@"special_price"]=set_price;
  259. item[@"price"]=get_price;
  260. item[@"name"]=name;
  261. item[@"description"]=description;
  262. item[@"detail"]=detail;
  263. item[@"img"]=img_path;
  264. // @{
  265. // //@"linenotes": line_note,
  266. //// @"product_id": product_id_string,
  267. //// @"available_qty": @(qty),
  268. //// @"available_percent" : @(percent),
  269. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  270. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  271. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  272. // @"": ,
  273. // @"": ,
  274. // @"": ,
  275. // @"": ,
  276. // @"": ,
  277. // @"": ,
  278. // @"":
  279. // }.mutableCopy;
  280. // if (percentage) {
  281. // [item removeObjectForKey:@"available_qty"];
  282. // } else {
  283. // [item removeObjectForKey:@"available_percent"];
  284. // }
  285. //
  286. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  287. // if ([qty_null isEqualToString:@"null"]) {
  288. // [item removeObjectForKey:@"available_qty"];
  289. // }
  290. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  291. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  292. }];
  293. [iSalesDB close_db:db];
  294. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  295. // [dic setValue:@"" forKey:@"email_content"];
  296. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  297. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  298. //
  299. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  300. // cell[@"count"]=[NSNumber numberWithInt:10];
  301. grid[@"cell0"]=dic;
  302. data[@"grid0"]=grid;
  303. return data;
  304. }
  305. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  306. {
  307. NSString *path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  308. NSData *data = [NSData dataWithContentsOfFile:path];
  309. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  310. return ret;
  311. }
  312. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  313. {
  314. NSString* template_file=nil;
  315. switch([params[@"pdf_style"] intValue])
  316. {
  317. case 0:
  318. template_file= @"portfolio_2x3.json";
  319. break;
  320. case 1:
  321. template_file= @"portfolio_3x2.json";
  322. break;
  323. default:
  324. template_file= @"portfolio_3x2.json";
  325. }
  326. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  327. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  328. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  329. DebugLog(@"%@",file);
  330. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  331. [dic setObject:@"Regular Mode" forKey:@"mode"];
  332. if (file) {
  333. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  334. [dic setObject:file forKey:@"pdf_path"];
  335. dic[@"isLocalFile"]=@"true";
  336. return [RAUtils dict2data:dic];
  337. }
  338. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  339. dic[@"isLocalFile"]=@"true";
  340. return [RAUtils dict2data:dic];
  341. }
  342. +(NSString*) get_offline_soid:(sqlite3*)db
  343. {
  344. NSString* soid=nil;
  345. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  346. [formatter setDateFormat:@"yyMMdd"];
  347. NSString* date = [formatter stringFromDate:[NSDate date]];
  348. for(int i=1;i<999;i++)
  349. {
  350. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  351. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  352. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  353. if(count==0)
  354. return soid;
  355. }
  356. return nil;
  357. }
  358. +(NSArray*) enumOfflineOrder
  359. {
  360. NSMutableArray* ret = [[NSMutableArray alloc] init];
  361. sqlite3 *db = [iSalesDB get_db];
  362. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  363. sqlite3_stmt * statement;
  364. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  365. if ( dbresult== SQLITE_OK)
  366. {
  367. while (sqlite3_step(statement) == SQLITE_ROW)
  368. {
  369. char *so_id = (char*)sqlite3_column_text(statement, 0);
  370. NSString* nsso_id=nil;
  371. if(so_id!=nil)
  372. {
  373. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  374. }
  375. [ret addObject:nsso_id];
  376. }
  377. sqlite3_finalize(statement);
  378. }
  379. [iSalesDB close_db:db];
  380. return ret;
  381. }
  382. +(void) uploadFile:(NSString*) file
  383. {
  384. NSData* data = [NSData dataWithContentsOfFile: file];
  385. UIApplication * app = [UIApplication sharedApplication];
  386. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  387. AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  388. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  389. if(appDelegate.user!=nil)
  390. [params setValue:appDelegate.user forKey:@"user"];
  391. // if(appDelegate.contact_id!=nil)
  392. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  393. if(appDelegate.password!=nil)
  394. [params setValue:appDelegate.password forKey:@"password"];
  395. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  396. [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  397. } error:nil];
  398. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  399. NSProgress *progress = nil;
  400. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  401. if (error) {
  402. NSString* err_msg = [error localizedDescription];
  403. NSLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  404. NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  405. NSLog(@"data string: %@",str);
  406. [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  407. } else {
  408. NSLog(@"response ");
  409. NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  410. // 再将NSData转为字符串
  411. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  412. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  413. NSLog(@"data string: %@",jsonStr);
  414. NSDictionary* json = responseObject;
  415. if([[json valueForKey:@"result"] intValue]==2)
  416. {
  417. // NSString* img_url_down = json[@"img_url_aname"];
  418. // NSString* img_url_up = json[@"img_url"];
  419. }
  420. else
  421. {
  422. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  423. }
  424. }
  425. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  426. }];
  427. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  428. //
  429. [uploadTask resume];
  430. }
  431. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  432. {
  433. NSString* serial= [[NSUUID UUID] UUIDString];
  434. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  435. NSString *cachefolder = [paths objectAtIndex:0];
  436. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  437. upfolder = [upfolder stringByAppendingPathComponent:serial];
  438. NSFileManager* fileManager = [NSFileManager defaultManager];
  439. BOOL bdir=YES;
  440. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  441. {
  442. NSError *error = nil;
  443. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  444. if(!bsuccess)
  445. DebugLog(@"Create UPLOAD folder failed");
  446. }
  447. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  448. ret[@"contact"]=[self prepareContact:serial];
  449. ret[@"wishlist"]=[self prepareWishlist:serial];
  450. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  451. ret[@"portfolio"] = [self preparePortfolio:serial];
  452. ret[@"view_portfolio"] = [self preparePDF:serial];
  453. NSString* str= [RAUtils dict2string:ret];
  454. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  455. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  456. NSError *error=nil;
  457. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  458. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  459. ZipArchive* zip = [[ZipArchive alloc] init];
  460. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  461. for(int i=0;i<arr_files.count;i++)
  462. {
  463. NSString* file=arr_files[i];
  464. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  465. }
  466. if( ![zip CloseZipFile2] )
  467. {
  468. zippath = @"";
  469. }
  470. ret[@"file"]=zippath;
  471. return ret;
  472. }
  473. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  474. {
  475. if(filename.length==0)
  476. return false;
  477. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  478. bool ret=false;
  479. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  480. NSString *cachefolder = [paths objectAtIndex:0];
  481. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  482. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  483. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  484. upfolder = [upfolder stringByAppendingPathComponent:serial];
  485. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  486. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  487. NSFileManager* fileManager = [NSFileManager defaultManager];
  488. BOOL bdir=NO;
  489. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  490. {
  491. NSError *error = nil;
  492. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  493. {
  494. ret=false;
  495. }
  496. else
  497. {
  498. ret=true;
  499. }
  500. }
  501. return ret;
  502. }
  503. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  504. {
  505. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  506. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  507. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  508. NSString* where=@" 1=1";
  509. if (ver!=nil) {
  510. where=@"is_dirty=1";
  511. }
  512. sqlite3 *db = [iSalesDB get_db];
  513. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  514. sqlite3_stmt * statement;
  515. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  516. int count=0;
  517. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  518. if ( dbresult== SQLITE_OK)
  519. {
  520. while (sqlite3_step(statement) == SQLITE_ROW)
  521. {
  522. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  523. int _id = sqlite3_column_int(statement, 0);
  524. int product_id = sqlite3_column_int(statement, 1);
  525. int item_id = sqlite3_column_int(statement, 2);
  526. int qty = sqlite3_column_int(statement, 3);
  527. char *create_time = (char*)sqlite3_column_text(statement, 4);
  528. NSString* nscreate_time=nil;
  529. if(create_time!=nil)
  530. {
  531. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  532. }
  533. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  534. NSString* nsmodify_time=nil;
  535. if(modify_time!=nil)
  536. {
  537. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  538. }
  539. int is_delete = sqlite3_column_int(statement, 6);
  540. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  541. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  542. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  543. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  544. itemjson[@"create_time"]=nscreate_time;
  545. itemjson[@"modify_time"]=nsmodify_time;
  546. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  547. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  548. count++;
  549. }
  550. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  551. sqlite3_finalize(statement);
  552. }
  553. ret[@"count"]=[NSNumber numberWithInt:count ];
  554. [iSalesDB close_db:db];
  555. return ret;
  556. }
  557. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  558. {
  559. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  560. UIApplication * app = [UIApplication sharedApplication];
  561. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  562. 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 ];
  563. // 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 ];
  564. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  565. DebugLog(@"offline_login sql:%@",sqlQuery);
  566. sqlite3_stmt * statement;
  567. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  568. int count=0;
  569. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  570. if ( dbresult== SQLITE_OK)
  571. {
  572. while (sqlite3_step(statement) == SQLITE_ROW)
  573. {
  574. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  575. int product_id = sqlite3_column_int(statement, 0);
  576. char *str_price = (char*)sqlite3_column_text(statement, 1);
  577. int item_id = sqlite3_column_int(statement, 7);
  578. NSString* Price=nil;
  579. if(str_price==nil)
  580. {
  581. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  582. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  583. if(price==nil)
  584. Price=@"No Price.";
  585. else
  586. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  587. }
  588. else
  589. {
  590. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  591. }
  592. double discount = sqlite3_column_double(statement, 2);
  593. int item_count = sqlite3_column_int(statement, 3);
  594. char *line_note = (char*)sqlite3_column_text(statement, 4);
  595. NSString *nsline_note=nil;
  596. if(line_note!=nil)
  597. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  598. char *name = (char*)sqlite3_column_text(statement, 5);
  599. NSString *nsname=nil;
  600. if(name!=nil)
  601. nsname= [[NSString alloc]initWithUTF8String:name];
  602. char *description = (char*)sqlite3_column_text(statement, 6);
  603. NSString *nsdescription=nil;
  604. if(description!=nil)
  605. nsdescription= [[NSString alloc]initWithUTF8String:description];
  606. // int stockUom = sqlite3_column_int(statement, 8);
  607. // int _id = sqlite3_column_int(statement, 9);
  608. //
  609. //
  610. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  611. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  612. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  613. // int carton=[bsubtotaljson[@"carton"] intValue];
  614. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  615. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  616. itemjson[@"model"]=nsname;
  617. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  618. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  619. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  620. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  621. // itemjson[@"check"]=@"true";
  622. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  623. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  624. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  625. itemjson[@"unit_price"]=Price;
  626. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  627. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  628. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  629. itemjson[@"note"]=nsline_note;
  630. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  631. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  632. count++;
  633. }
  634. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  635. sqlite3_finalize(statement);
  636. }
  637. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  638. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  639. return ret;
  640. }
  641. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  642. {
  643. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  644. sqlite3 *db = [iSalesDB get_db];
  645. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  646. // for(int i=0;i<arr_soid.count;i++)
  647. sqlite3_stmt * statement;
  648. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  649. //int count=0;
  650. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  651. if ( dbresult== SQLITE_OK)
  652. {
  653. int count=0;
  654. while (sqlite3_step(statement) == SQLITE_ROW)
  655. {
  656. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  657. int _id = sqlite3_column_int(statement, 0);
  658. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  659. NSString* nssync_data=nil;
  660. if(sync_data!=nil)
  661. {
  662. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  663. }
  664. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  665. NSString* nsimg_1=nil;
  666. if(img_1!=nil)
  667. {
  668. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  669. [self copy_upImg:serial file:nsimg_1];
  670. }
  671. char *so_no = (char*)sqlite3_column_text(statement, 3);
  672. NSString* nsso_no=nil;
  673. if(so_no!=nil)
  674. {
  675. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  676. }
  677. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  678. itemjson[@"order_type"]=@"submit order";
  679. else
  680. itemjson[@"order_type"]=@"archive order";
  681. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  682. itemjson[@"json_data"]= nssync_data;
  683. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  684. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  685. count++;
  686. }
  687. ret[@"count"]=[NSNumber numberWithInt:count ];
  688. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  689. }
  690. sqlite3_finalize(statement);
  691. [iSalesDB close_db:db];
  692. return ret;
  693. }
  694. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  695. {
  696. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  697. sqlite3 *db = [iSalesDB get_db];
  698. // UIApplication * app = [UIApplication sharedApplication];
  699. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  700. 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";
  701. sqlite3_stmt * statement;
  702. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  703. int count=0;
  704. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  705. if ( dbresult== SQLITE_OK)
  706. {
  707. while (sqlite3_step(statement) == SQLITE_ROW)
  708. {
  709. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  710. int _id = sqlite3_column_int(statement, 0);
  711. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  712. NSString* nsimg_0=nil;
  713. if(img_0!=nil)
  714. {
  715. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  716. [self copy_upImg:serial file:nsimg_0];
  717. }
  718. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  719. NSString* nsimg_1=nil;
  720. if(img_1!=nil)
  721. {
  722. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  723. [self copy_upImg:serial file:nsimg_1];
  724. }
  725. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  726. NSString* nsimg_2=nil;
  727. if(img_2!=nil)
  728. {
  729. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  730. [self copy_upImg:serial file:nsimg_2];
  731. }
  732. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  733. NSString* nssync_data=nil;
  734. if(sync_data!=nil)
  735. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  736. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  737. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  738. itemjson[@"json_data"]= nssync_data;
  739. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  740. count++;
  741. }
  742. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  743. sqlite3_finalize(statement);
  744. }
  745. ret[@"count"]=[NSNumber numberWithInt:count ];
  746. [iSalesDB close_db:db];
  747. return ret;
  748. }
  749. +(bool) check_offlinedata
  750. {
  751. UIApplication * app = [UIApplication sharedApplication];
  752. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  753. sqlite3 *db = [iSalesDB get_db];
  754. NSString * where=@"1=1";
  755. // if(appDelegate.user!=nil)
  756. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  757. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  758. [iSalesDB close_db:db];
  759. if(count==0)
  760. {
  761. return false;
  762. }
  763. return true;
  764. //
  765. //[iSalesDB close_db:db];
  766. }
  767. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  768. {
  769. UIApplication * app = [UIApplication sharedApplication];
  770. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  771. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  772. sqlite3 *db = [iSalesDB get_db];
  773. NSString* collectId=params[@"collectId"];
  774. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  775. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  776. [iSalesDB execSql:sqlQuery db:db];
  777. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  778. [iSalesDB close_db:db];
  779. appDelegate.wish_count =count;
  780. [appDelegate update_count_mark];
  781. ret[@"result"]= [NSNumber numberWithInt:2];
  782. return ret;
  783. }
  784. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  785. //{
  786. //
  787. // UIApplication * app = [UIApplication sharedApplication];
  788. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  789. //
  790. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  791. // sqlite3 *db = [iSalesDB get_db];
  792. // NSString* product_id=params[@"product_id"];
  793. //
  794. //
  795. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  796. //
  797. //
  798. // for(int i=0;i<arr.count;i++)
  799. // {
  800. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  801. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  802. // if(count==0)
  803. // {
  804. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  805. // [iSalesDB execSql:sqlQuery db:db];
  806. // }
  807. // }
  808. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  809. // [iSalesDB close_db:db];
  810. //
  811. // appDelegate.wish_count =count;
  812. //
  813. // [appDelegate update_count_mark];
  814. // ret[@"result"]= [NSNumber numberWithInt:2];
  815. // return ret;
  816. // //
  817. // //return ret;
  818. //}
  819. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  820. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  821. DebugLog(@"time interval: %lf",interval);
  822. }
  823. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  824. {
  825. UIApplication * app = [UIApplication sharedApplication];
  826. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  827. int sort = [[params objectForKey:@"sort"] intValue];
  828. NSString *sort_str = @"";
  829. switch (sort) {
  830. case 0:{
  831. sort_str = @"order by w.modify_time desc";
  832. }
  833. break;
  834. case 1:{
  835. sort_str = @"order by w.modify_time asc";
  836. }
  837. break;
  838. case 2:{
  839. sort_str = @"order by m.name asc";
  840. }
  841. break;
  842. case 3:{
  843. sort_str = @"order by m.name desc";
  844. }
  845. break;
  846. case 4:{
  847. sort_str = @"order by m.description asc";
  848. }
  849. break;
  850. default:
  851. break;
  852. }
  853. NSString* user = appDelegate.user;
  854. sqlite3 *db = [iSalesDB get_db];
  855. // order by w.create_time
  856. 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];
  857. // 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];
  858. sqlite3_stmt * statement;
  859. NSDate *date1 = [NSDate date];
  860. NSDate *date2 = nil;
  861. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  862. int count=0;
  863. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  864. {
  865. while (sqlite3_step(statement) == SQLITE_ROW)
  866. {
  867. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  868. int product_id = sqlite3_column_double(statement, 0);
  869. char *description = (char*)sqlite3_column_text(statement, 1);
  870. if(description==nil)
  871. description= "";
  872. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  873. int item_id = sqlite3_column_double(statement, 2);
  874. NSDate *date_image = [NSDate date];
  875. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  876. printf("image : ");
  877. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  878. // char *url = (char*)sqlite3_column_text(statement, 3);
  879. // if(url==nil)
  880. // url="";
  881. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  882. int qty = sqlite3_column_int(statement, 3);
  883. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  884. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  885. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  886. item[@"description"]= nsdescription;
  887. item[@"img"]= nsurl;
  888. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  889. count++;
  890. }
  891. printf("total time:");
  892. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  893. ret[@"count"]= [NSNumber numberWithInt:count];
  894. ret[@"total_count"]= [NSNumber numberWithInt:count];
  895. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  896. ret[@"result"]= [NSNumber numberWithInt:2];
  897. appDelegate.wish_count =count;
  898. [appDelegate update_count_mark];
  899. sqlite3_finalize(statement);
  900. }
  901. [iSalesDB close_db:db];
  902. return ret;
  903. }
  904. +(NSDictionary*) offline_notimpl
  905. {
  906. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  907. ret[@"result"]=@"8";
  908. ret[@"err_msg"]=@"offline mode does not support this function.";
  909. return ret;
  910. }
  911. +(NSDictionary*) offline_home
  912. {
  913. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  914. NSString *cachefolder = [paths objectAtIndex:0];
  915. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  916. NSData* json =nil;
  917. json=[NSData dataWithContentsOfFile:img_cache];
  918. NSError *error=nil;
  919. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  920. return menu;
  921. }
  922. +(NSDictionary*) offline_category_menu
  923. {
  924. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  925. NSString *cachefolder = [paths objectAtIndex:0];
  926. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  927. NSData* json =nil;
  928. json=[NSData dataWithContentsOfFile:img_cache];
  929. NSError *error=nil;
  930. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  931. return menu;
  932. }
  933. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  934. {
  935. NSString* offline_command=params[@"offline_Command"];
  936. NSDictionary* ret=nil;
  937. if([offline_command isEqualToString:@"model_NIYMAL"])
  938. {
  939. NSString* category = params[@"category"];
  940. ret = [self refresh_model_NIYMAL:category];
  941. }
  942. return ret;
  943. }
  944. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  945. {
  946. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  947. [ret setValue:@"2" forKey:@"result"];
  948. [ret setValue:@"Regular Mode" forKey:@"mode"];
  949. sqlite3* db= [iSalesDB get_db];
  950. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  951. [iSalesDB close_db:db];
  952. [ret setObject:detail1_section forKey:@"detail_1"];
  953. return ret;
  954. }
  955. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  956. {
  957. UIApplication * app = [UIApplication sharedApplication];
  958. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  959. // NSArray* arr1 = [self get_user_all_price_type];
  960. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  961. // NSSet *set1 = [NSSet setWithArray:arr1];
  962. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  963. // if(appDelegate.contact_id==nil)
  964. // set2=[set1 mutableCopy];
  965. // else
  966. // [set2 intersectsSet:set1];
  967. // NSArray *retarr = [set2 allObjects];
  968. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  969. // sqlite3 *db = [iSalesDB get_db];
  970. NSString* sqlQuery = nil;
  971. if(appDelegate.contact_id==nil)
  972. 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];
  973. else
  974. 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];
  975. sqlite3_stmt * statement;
  976. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  977. int count=0;
  978. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  979. {
  980. while (sqlite3_step(statement) == SQLITE_ROW)
  981. {
  982. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  983. char *name = (char*)sqlite3_column_text(statement, 0);
  984. if(name==nil)
  985. name="";
  986. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  987. // double price = sqlite3_column_double(statement, 1);
  988. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  989. // if(isnull==nil)
  990. // item[nsname]= @"No Price";
  991. // else
  992. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  993. char *price = (char*)sqlite3_column_text(statement, 1);
  994. if(price!=nil)
  995. {
  996. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  997. nsprice=[AESCrypt fastdecrypt:nsprice];
  998. if(nsprice.length>0)
  999. {
  1000. double dp= [nsprice doubleValue];
  1001. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  1002. }
  1003. }
  1004. else
  1005. {
  1006. item[nsname]= @"No Price";
  1007. }
  1008. // int type= sqlite3_column_int(statement, 2);
  1009. // item[@"type"]=[NSNumber numberWithInt:type ];
  1010. // item[nsname]= nsprice;
  1011. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1012. count++;
  1013. }
  1014. ret[@"count"]= [NSNumber numberWithInt:count];
  1015. sqlite3_finalize(statement);
  1016. }
  1017. // [iSalesDB close_db:db];
  1018. return ret;
  1019. }
  1020. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  1021. //{
  1022. // NSArray* arr1 = [self get_user_all_price_type:db];
  1023. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1024. //
  1025. // // NSSet *set1 = [NSSet setWithArray:arr1];
  1026. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1027. // // [set2 intersectsSet:set1];
  1028. // //
  1029. // //
  1030. // // NSArray *retarr = [set2 allObjects];
  1031. //
  1032. // NSString* whereprice=nil;
  1033. // if(contact_id==nil)
  1034. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1035. // else
  1036. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1037. //
  1038. //
  1039. // // sqlite3 *db = [iSalesDB get_db];
  1040. //
  1041. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  1042. // sqlite3_stmt * statement;
  1043. //
  1044. //
  1045. // NSNumber* ret = nil;
  1046. // double dprice=DBL_MAX;
  1047. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1048. // {
  1049. //
  1050. //
  1051. // while (sqlite3_step(statement) == SQLITE_ROW)
  1052. // {
  1053. //
  1054. // // double val = sqlite3_column_double(statement, 0);
  1055. // char *price = (char*)sqlite3_column_text(statement, 0);
  1056. // if(price!=nil)
  1057. // {
  1058. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1059. // nsprice=[AESCrypt fastdecrypt:nsprice];
  1060. // if(nsprice.length>0)
  1061. // {
  1062. // double dp= [nsprice doubleValue];
  1063. // if(dp<dprice)
  1064. // dprice=dp;
  1065. // }
  1066. // }
  1067. // }
  1068. //
  1069. //
  1070. //
  1071. //
  1072. // sqlite3_finalize(statement);
  1073. //
  1074. //
  1075. //
  1076. //
  1077. // }
  1078. //
  1079. // // [iSalesDB close_db:db];
  1080. //
  1081. // if(dprice==DBL_MAX)
  1082. // ret= nil;
  1083. // else
  1084. // ret= [NSNumber numberWithDouble:dprice];
  1085. // return ret;
  1086. //}
  1087. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  1088. {
  1089. NSArray* arr1 = [self get_user_all_price_type:db];
  1090. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1091. NSString* whereprice=nil;
  1092. if(contact_id==nil)
  1093. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1094. else
  1095. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1096. // sqlite3 *db = [iSalesDB get_db];
  1097. NSString *productIdCondition = @"1 = 1";
  1098. if (product_id) {
  1099. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  1100. }
  1101. NSString *itemIdCondition = @"";
  1102. if (item_id) {
  1103. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  1104. }
  1105. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  1106. sqlite3_stmt * statement;
  1107. NSNumber* ret = nil;
  1108. double dprice=DBL_MAX;
  1109. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1110. {
  1111. while (sqlite3_step(statement) == SQLITE_ROW)
  1112. {
  1113. // double val = sqlite3_column_double(statement, 0);
  1114. char *price = (char*)sqlite3_column_text(statement, 0);
  1115. if(price!=nil)
  1116. {
  1117. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1118. nsprice=[AESCrypt fastdecrypt:nsprice];
  1119. if(nsprice.length>0)
  1120. {
  1121. double dp= [nsprice doubleValue];
  1122. if(dp<dprice)
  1123. dprice=dp;
  1124. }
  1125. }
  1126. }
  1127. sqlite3_finalize(statement);
  1128. }
  1129. // [iSalesDB close_db:db];
  1130. if(dprice==DBL_MAX)
  1131. ret= nil;
  1132. else
  1133. ret= [NSNumber numberWithDouble:dprice];
  1134. return ret;
  1135. }
  1136. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  1137. {
  1138. NSArray* ret=nil;
  1139. // sqlite3 *db = [iSalesDB get_db];
  1140. // no customer assigned , use login user contact_id
  1141. UIApplication * app = [UIApplication sharedApplication];
  1142. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1143. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  1144. sqlite3_stmt * statement;
  1145. // int count=0;
  1146. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1147. {
  1148. if (sqlite3_step(statement) == SQLITE_ROW)
  1149. {
  1150. char *val = (char*)sqlite3_column_text(statement, 0);
  1151. if(val==nil)
  1152. val="";
  1153. NSString* price = [[NSString alloc]initWithUTF8String:val];
  1154. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  1155. }
  1156. sqlite3_finalize(statement);
  1157. }
  1158. // [iSalesDB close_db:db];
  1159. return ret;
  1160. }
  1161. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  1162. {
  1163. // sqlite3 *db = [iSalesDB get_db];
  1164. if(contact_id==nil)
  1165. {
  1166. // no customer assigned , use login user contact_id
  1167. UIApplication * app = [UIApplication sharedApplication];
  1168. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1169. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  1170. sqlite3_stmt * statement;
  1171. // int count=0;
  1172. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1173. {
  1174. if (sqlite3_step(statement) == SQLITE_ROW)
  1175. {
  1176. char *val = (char*)sqlite3_column_text(statement, 0);
  1177. if(val==nil)
  1178. val="";
  1179. contact_id = [[NSString alloc]initWithUTF8String:val];
  1180. }
  1181. sqlite3_finalize(statement);
  1182. }
  1183. if(contact_id.length<=0)
  1184. {
  1185. // [iSalesDB close_db:db];
  1186. return nil;
  1187. }
  1188. }
  1189. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  1190. sqlite3_stmt * statement;
  1191. NSArray* ret=nil;
  1192. // int count=0;
  1193. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1194. {
  1195. if (sqlite3_step(statement) == SQLITE_ROW)
  1196. {
  1197. char *price_type = (char*)sqlite3_column_text(statement, 0);
  1198. if(price_type==nil)
  1199. price_type="";
  1200. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  1201. if(nsprice_type.length>0)
  1202. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  1203. else
  1204. ret=nil;
  1205. }
  1206. sqlite3_finalize(statement);
  1207. }
  1208. // [iSalesDB close_db:db];
  1209. return ret;
  1210. }
  1211. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  1212. {
  1213. NSString* ret= nil;
  1214. // sqlite3 *db = [iSalesDB get_db];
  1215. NSString *sqlQuery = nil;
  1216. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  1217. // 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;
  1218. // 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
  1219. if(product_id==nil && model_name)
  1220. 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;
  1221. else if (product_id)
  1222. 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
  1223. sqlite3_stmt * statement;
  1224. // int count=0;
  1225. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1226. {
  1227. if (sqlite3_step(statement) == SQLITE_ROW)
  1228. {
  1229. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  1230. if(imgurl==nil)
  1231. imgurl="";
  1232. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  1233. ret=nsimgurl;
  1234. }
  1235. sqlite3_finalize(statement);
  1236. }
  1237. else
  1238. {
  1239. [ret setValue:@"8" forKey:@"result"];
  1240. }
  1241. // [iSalesDB close_db:db];
  1242. DebugLog(@"data string: %@",ret );
  1243. return ret;
  1244. }
  1245. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  1246. {
  1247. NSString* ret= nil;
  1248. sqlite3 *db = [iSalesDB get_db];
  1249. NSString *sqlQuery = nil;
  1250. if(product_id==nil)
  1251. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  1252. else
  1253. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  1254. sqlite3_stmt * statement;
  1255. // int count=0;
  1256. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1257. {
  1258. if (sqlite3_step(statement) == SQLITE_ROW)
  1259. {
  1260. char *default_category = (char*)sqlite3_column_text(statement, 0);
  1261. if(default_category==nil)
  1262. default_category="";
  1263. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1264. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  1265. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  1266. ret=nsdefault_category;
  1267. }
  1268. sqlite3_finalize(statement);
  1269. }
  1270. else
  1271. {
  1272. [ret setValue:@"8" forKey:@"result"];
  1273. }
  1274. [iSalesDB close_db:db];
  1275. DebugLog(@"data string: %@",ret );
  1276. return ret;
  1277. }
  1278. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  1279. {
  1280. NSString* model_name = [params valueForKey:@"product_name"];
  1281. NSString* product_id = [params valueForKey:@"product_id"];
  1282. NSString* category = [params valueForKey:@"category"];
  1283. if(category==nil)
  1284. category = [self model_default_category:product_id model_name:model_name];
  1285. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1286. sqlite3 *db = [iSalesDB get_db];
  1287. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  1288. NSString *sqlQuery = nil;
  1289. if(product_id==nil)
  1290. 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='%@';
  1291. else
  1292. 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=%@;
  1293. sqlite3_stmt * statement;
  1294. [ret setValue:@"2" forKey:@"result"];
  1295. [ret setValue:@"3" forKey:@"detail_section_count"];
  1296. // int count=0;
  1297. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1298. {
  1299. if (sqlite3_step(statement) == SQLITE_ROW)
  1300. {
  1301. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1302. char *name = (char*)sqlite3_column_text(statement, 0);
  1303. if(name==nil)
  1304. name="";
  1305. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1306. char *description = (char*)sqlite3_column_text(statement, 1);
  1307. if(description==nil)
  1308. description="";
  1309. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1310. int product_id = sqlite3_column_int(statement, 2);
  1311. char *color = (char*)sqlite3_column_text(statement, 3);
  1312. if(color==nil)
  1313. color="";
  1314. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  1315. //
  1316. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  1317. // if(legcolor==nil)
  1318. // legcolor="";
  1319. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  1320. //
  1321. //
  1322. int availability = sqlite3_column_int(statement, 5);
  1323. //
  1324. int incoming_stock = sqlite3_column_int(statement, 6);
  1325. char *demension = (char*)sqlite3_column_text(statement, 7);
  1326. if(demension==nil)
  1327. demension="";
  1328. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  1329. // ,,,,,,,,,
  1330. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  1331. if(seat_height==nil)
  1332. seat_height="";
  1333. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  1334. char *material = (char*)sqlite3_column_text(statement, 9);
  1335. if(material==nil)
  1336. material="";
  1337. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  1338. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  1339. if(box_dim==nil)
  1340. box_dim="";
  1341. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  1342. char *volume = (char*)sqlite3_column_text(statement, 11);
  1343. if(volume==nil)
  1344. volume="";
  1345. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  1346. double weight = sqlite3_column_double(statement, 12);
  1347. char *model_set = (char*)sqlite3_column_text(statement, 13);
  1348. if(model_set==nil)
  1349. model_set="";
  1350. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  1351. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  1352. if(load_ability==nil)
  1353. load_ability="";
  1354. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  1355. char *default_category = (char*)sqlite3_column_text(statement, 15);
  1356. if(default_category==nil)
  1357. default_category="";
  1358. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1359. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  1360. if(fabric_content==nil)
  1361. fabric_content="";
  1362. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  1363. char *assembling = (char*)sqlite3_column_text(statement, 17);
  1364. if(assembling==nil)
  1365. assembling="";
  1366. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  1367. char *made_in = (char*)sqlite3_column_text(statement, 18);
  1368. if(made_in==nil)
  1369. made_in="";
  1370. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  1371. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  1372. if(special_remarks==nil)
  1373. special_remarks="";
  1374. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  1375. int stockUcom = sqlite3_column_double(statement, 20);
  1376. char *product_group = (char*)sqlite3_column_text(statement, 21);
  1377. if(product_group==nil)
  1378. product_group="";
  1379. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  1380. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  1381. // if(fashion_selector==nil)
  1382. // fashion_selector="";
  1383. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  1384. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  1385. if(selector_field==nil)
  1386. selector_field="";
  1387. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  1388. char *property_field = (char*)sqlite3_column_text(statement, 23);
  1389. if(property_field==nil)
  1390. property_field="";
  1391. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  1392. char *packaging = (char*)sqlite3_column_text(statement, 24);
  1393. if(packaging==nil)
  1394. packaging="";
  1395. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  1396. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1397. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  1398. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  1399. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  1400. [img_section setValue:model_s_img forKey:@"model_s_img"];
  1401. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  1402. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  1403. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  1404. NSString* Availability=nil;
  1405. if(availability>0)
  1406. Availability=[NSString stringWithFormat:@"%d",availability];
  1407. else
  1408. Availability = @"In Production";
  1409. [img_section setValue:Availability forKey:@"Availability"];
  1410. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  1411. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1412. char *eta = (char*)sqlite3_column_text(statement, 25);
  1413. if(eta==nil)
  1414. eta="";
  1415. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  1416. [img_section setValue:nseta forKey:@"ETA"];
  1417. int item_id = sqlite3_column_int(statement, 26);
  1418. NSString* Price=nil;
  1419. if(appDelegate.bLogin==false)
  1420. Price=@"Must Sign in.";
  1421. else
  1422. {
  1423. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1424. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1425. if(price==nil)
  1426. Price=@"No Price.";
  1427. else
  1428. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1429. }
  1430. [img_section setValue:Price forKey:@"price"];
  1431. [img_section setValue:nsname forKey:@"model_name"];
  1432. [img_section setValue:nsdescription forKey:@"model_descrition"];
  1433. [ret setObject:img_section forKey:@"img_section"];
  1434. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  1435. int detail0_item_count=0;
  1436. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1437. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1438. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1439. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1440. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1441. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1442. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1443. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1444. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1445. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1446. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1447. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1448. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1449. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  1450. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  1451. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  1452. {
  1453. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  1454. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1455. }
  1456. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  1457. [detail0_section setValue:@"kv" forKey:@"type"];
  1458. [detail0_section setValue:@"Product Information" forKey:@"title"];
  1459. [ret setObject:detail0_section forKey:@"detail_0"];
  1460. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1461. // [detail1_section setValue:@"detail" forKey:@"target"];
  1462. // [detail1_section setValue:@"popup" forKey:@"action"];
  1463. // [detail1_section setValue:@"content" forKey:@"type"];
  1464. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  1465. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1466. // [detail1_section setValue:@"true" forKey:@"single_row"];
  1467. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  1468. [ret setObject:detail1_section forKey:@"detail_1"];
  1469. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  1470. [detail2_section setValue:@"detail" forKey:@"target"];
  1471. [detail2_section setValue:@"popup" forKey:@"action"];
  1472. [detail2_section setValue:@"content" forKey:@"type"];
  1473. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  1474. [detail2_section setValue:@"true" forKey:@"single_row"];
  1475. [detail2_section setValue:@"local" forKey:@"data"];
  1476. [ret setObject:detail2_section forKey:@"detail_2"];
  1477. }
  1478. sqlite3_finalize(statement);
  1479. }
  1480. else
  1481. {
  1482. [ret setValue:@"8" forKey:@"result"];
  1483. }
  1484. NSLog(@"count:%d",count);
  1485. [iSalesDB close_db:db];
  1486. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1487. return ret;
  1488. }
  1489. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  1490. NSString* orderCode = [params valueForKey:@"orderCode"];
  1491. NSString* keyword = [params valueForKey:@"keyword"];
  1492. keyword=keyword.lowercaseString;
  1493. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  1494. int limit = [[params valueForKey:@"limit"] intValue];
  1495. int offset = [[params valueForKey:@"offset"] intValue];
  1496. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1497. NSString *limit_str = @"";
  1498. if (limited) {
  1499. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  1500. }
  1501. sqlite3 *db = [iSalesDB get_db];
  1502. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  1503. UIApplication * app = [UIApplication sharedApplication];
  1504. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1505. NSString *sqlQuery = nil;
  1506. if(exactMatch )
  1507. 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 ;
  1508. else
  1509. 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
  1510. DebugLog(@"offline_search sql:%@",sqlQuery);
  1511. sqlite3_stmt * statement;
  1512. [ret setValue:@"2" forKey:@"result"];
  1513. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  1514. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  1515. // int count=0;
  1516. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1517. {
  1518. int i=0;
  1519. while (sqlite3_step(statement) == SQLITE_ROW)
  1520. {
  1521. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1522. // char *name = (char*)sqlite3_column_text(statement, 1);
  1523. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1524. char *name = (char*)sqlite3_column_text(statement, 0);
  1525. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1526. char *description = (char*)sqlite3_column_text(statement, 1);
  1527. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1528. int product_id = sqlite3_column_int(statement, 2);
  1529. // char *url = (char*)sqlite3_column_text(statement, 3);
  1530. // if(url==nil)
  1531. // url="";
  1532. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1533. int wid = sqlite3_column_int(statement, 3);
  1534. int closeout = sqlite3_column_int(statement, 4);
  1535. int cid = sqlite3_column_int(statement, 5);
  1536. int wisdelete = sqlite3_column_int(statement, 6);
  1537. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  1538. if(wid !=0 && wisdelete != 1)
  1539. [item setValue:@"true" forKey:@"wish_exists"];
  1540. else
  1541. [item setValue:@"false" forKey:@"wish_exists"];
  1542. if(closeout==0)
  1543. [item setValue:@"false" forKey:@"is_closeout"];
  1544. else
  1545. [item setValue:@"true" forKey:@"is_closeout"];
  1546. if(cid==0)
  1547. [item setValue:@"false" forKey:@"cart_exists"];
  1548. else
  1549. [item setValue:@"true" forKey:@"cart_exists"];
  1550. [item addEntriesFromDictionary:imgjson];
  1551. // [item setValue:nsurl forKey:@"img"];
  1552. [item setValue:nsname forKey:@"fash_name"];
  1553. [item setValue:nsdescription forKey:@"description"];
  1554. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1555. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1556. i++;
  1557. }
  1558. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1559. [ret setObject:items forKey:@"items"];
  1560. sqlite3_finalize(statement);
  1561. }
  1562. NSLog(@"count:%d",count);
  1563. [iSalesDB close_db:db];
  1564. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1565. return ret;
  1566. }
  1567. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  1568. {
  1569. return [self search:params limited:YES];
  1570. }
  1571. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  1572. {
  1573. UIApplication * app = [UIApplication sharedApplication];
  1574. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1575. appDelegate.disable_trigger=true;
  1576. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1577. appDelegate.disable_trigger=false;
  1578. //
  1579. // NSString* user = [params valueForKey:@"user"];
  1580. //
  1581. // NSString* password = [params valueForKey:@"password"];
  1582. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1583. ret[@"result"]=[NSNumber numberWithInt:2 ];
  1584. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  1585. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  1586. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  1587. [appDelegate update_count_mark];
  1588. appDelegate.can_show_price =false;
  1589. appDelegate.can_see_price =false;
  1590. appDelegate.can_create_portfolio =false;
  1591. appDelegate.can_create_order =false;
  1592. appDelegate.can_cancel_order =false;
  1593. appDelegate.can_set_cart_price =false;
  1594. appDelegate.can_delete_order =false;
  1595. appDelegate.can_submit_order =false;
  1596. appDelegate.can_set_tearsheet_price =false;
  1597. appDelegate.can_update_contact_info = false;
  1598. appDelegate.save_order_logout = false;
  1599. appDelegate.submit_order_logout = false;
  1600. appDelegate.alert_sold_in_quantities = false;
  1601. appDelegate.ipad_perm =nil ;
  1602. appDelegate.user_type = USER_ROLE_UNKNOWN;
  1603. appDelegate.OrderFilter= nil;
  1604. [appDelegate SetSo:nil];
  1605. [appDelegate set_main_button_panel];
  1606. // sqlite3 *db = [iSalesDB get_db];
  1607. //
  1608. //
  1609. //
  1610. //
  1611. //
  1612. // 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"]];
  1613. //
  1614. //
  1615. //
  1616. //
  1617. // DebugLog(@"offline_login sql:%@",sqlQuery);
  1618. // sqlite3_stmt * statement;
  1619. //
  1620. //
  1621. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1622. //
  1623. //
  1624. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1625. // {
  1626. //
  1627. //
  1628. // if (sqlite3_step(statement) == SQLITE_ROW)
  1629. // {
  1630. //
  1631. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1632. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1633. //
  1634. //
  1635. //
  1636. // int can_show_price = sqlite3_column_int(statement, 0);
  1637. // int can_see_price = sqlite3_column_int(statement, 1);
  1638. //
  1639. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1640. // if(contact_id==nil)
  1641. // contact_id="";
  1642. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1643. //
  1644. // int user_type = sqlite3_column_int(statement, 3);
  1645. //
  1646. // int can_cancel_order = sqlite3_column_int(statement, 4);
  1647. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  1648. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  1649. // int can_delete_order = sqlite3_column_int(statement, 7);
  1650. // int can_submit_order = sqlite3_column_int(statement, 8);
  1651. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1652. // int can_create_order = sqlite3_column_int(statement, 10);
  1653. //
  1654. //
  1655. // char *mode = (char*)sqlite3_column_text(statement, 11);
  1656. // if(mode==nil)
  1657. // mode="";
  1658. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1659. //
  1660. //
  1661. // char *username = (char*)sqlite3_column_text(statement, 12);
  1662. // if(username==nil)
  1663. // username="";
  1664. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1665. //
  1666. //
  1667. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1668. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1669. // [header setValue:nscontact_id forKey:@"contact_id"];
  1670. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1671. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1672. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1673. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1674. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1675. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1676. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1677. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1678. //
  1679. // [header setValue:nsusername forKey:@"username"];
  1680. //
  1681. //
  1682. // [ret setObject:header forKey:@"header"];
  1683. // [ret setValue:nsmode forKey:@"mode"];
  1684. //
  1685. //
  1686. // }
  1687. //
  1688. //
  1689. //
  1690. // sqlite3_finalize(statement);
  1691. // }
  1692. //
  1693. //
  1694. //
  1695. // [iSalesDB close_db:db];
  1696. //
  1697. //
  1698. //
  1699. //
  1700. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1701. return ret;
  1702. }
  1703. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  1704. {
  1705. UIApplication * app = [UIApplication sharedApplication];
  1706. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1707. appDelegate.disable_trigger=true;
  1708. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1709. appDelegate.disable_trigger=false;
  1710. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1711. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  1712. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  1713. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  1714. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  1715. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  1716. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  1717. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  1718. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  1719. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  1720. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  1721. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  1722. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  1723. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  1724. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  1725. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  1726. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  1727. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  1728. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  1729. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  1730. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  1731. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  1732. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1733. [arr_name addObject:customer_first_name];
  1734. [arr_name addObject:customer_last_name];
  1735. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1736. // default ship from
  1737. 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';";
  1738. __block NSString *cid = @"";
  1739. __block NSString *name = @"";
  1740. __block NSString *ext = @"";
  1741. __block NSString *contact = @"";
  1742. __block NSString *email = @"";
  1743. __block NSString *fax = @"";
  1744. __block NSString *phone = @"";
  1745. sqlite3 *db = [iSalesDB get_db];
  1746. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  1747. cid = [self textAtColumn:0 statement:statment];
  1748. name = [self textAtColumn:1 statement:statment];
  1749. ext = [self textAtColumn:2 statement:statment];
  1750. contact = [self textAtColumn:3 statement:statment];
  1751. email = [self textAtColumn:4 statement:statment];
  1752. fax = [self textAtColumn:5 statement:statment];
  1753. phone = [self textAtColumn:6 statement:statment];
  1754. }];
  1755. NSString* so_id = [self get_offline_soid:db];
  1756. if(so_id==nil)
  1757. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  1758. 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) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@')",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];
  1759. int result =[iSalesDB execSql:sql_neworder db:db];
  1760. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  1761. //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'
  1762. //soId
  1763. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  1764. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  1765. sqlite3_stmt * statement;
  1766. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1767. {
  1768. if (sqlite3_step(statement) == SQLITE_ROW)
  1769. {
  1770. // char *name = (char*)sqlite3_column_text(statement, 1);
  1771. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1772. //ret = sqlite3_column_int(statement, 0);
  1773. char *soId = (char*)sqlite3_column_text(statement, 0);
  1774. if(soId==nil)
  1775. soId="";
  1776. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  1777. [ret setValue:nssoId forKey:@"soId"];
  1778. [ret setValue:nssoId forKey:@"orderCode"];
  1779. }
  1780. sqlite3_finalize(statement);
  1781. }
  1782. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  1783. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1784. [iSalesDB close_db:db];
  1785. return [RAUtils dict2data:ret];
  1786. }
  1787. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  1788. {
  1789. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1790. NSString* orderCode = [params valueForKey:@"orderCode"];
  1791. UIApplication * app = [UIApplication sharedApplication];
  1792. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1793. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  1794. {
  1795. appDelegate.disable_trigger=true;
  1796. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1797. appDelegate.disable_trigger=false;
  1798. }
  1799. sqlite3 *db = [iSalesDB get_db];
  1800. int cart_count=[self query_ordercartcount:orderCode db:db];
  1801. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1802. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1803. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1804. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1805. [iSalesDB close_db:db];
  1806. return [RAUtils dict2data:ret];
  1807. }
  1808. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  1809. {
  1810. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1811. sqlite3 *db = [iSalesDB get_db];
  1812. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  1813. int count =0;
  1814. if(params[@"count"]!=nil)
  1815. {
  1816. count = [params[@"count"] intValue];
  1817. }
  1818. NSString* product_id=params[@"product_id"];
  1819. NSString* orderCode=params[@"orderCode"];
  1820. NSString *qty = params[@"qty"];
  1821. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  1822. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  1823. for(int i=0;i<arr_id.count;i++)
  1824. {
  1825. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  1826. int item_qty= count;
  1827. if (qty) {
  1828. item_qty = [qty_arr[i] integerValue];
  1829. }
  1830. if(item_qty==0)
  1831. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  1832. NSString* sql=nil;
  1833. sqlite3_stmt *stmt;
  1834. if(_id<0)
  1835. {
  1836. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  1837. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  1838. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1839. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  1840. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  1841. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1842. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  1843. }
  1844. else
  1845. {
  1846. if (qty) {
  1847. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  1848. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1849. sqlite3_bind_int(stmt, 1, _id);
  1850. } else {
  1851. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  1852. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1853. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1854. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  1855. }
  1856. }
  1857. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  1858. [iSalesDB execSql:@"ROLLBACK" db:db];
  1859. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  1860. [iSalesDB close_db:db];
  1861. DebugLog(@"add to cart error");
  1862. return [RAUtils dict2data:ret];
  1863. }
  1864. }
  1865. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1866. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1867. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1868. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1869. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1870. [iSalesDB execSql:@"END TRANSACTION" db:db];
  1871. [iSalesDB close_db:db];
  1872. return [RAUtils dict2data:ret];
  1873. }
  1874. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute
  1875. {
  1876. UIApplication * app = [UIApplication sharedApplication];
  1877. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1878. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1879. 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];
  1880. sqlite3_stmt * statement;
  1881. int count=0;
  1882. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1883. {
  1884. while (sqlite3_step(statement) == SQLITE_ROW)
  1885. {
  1886. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  1887. int bitem_id = sqlite3_column_int(statement, 0);
  1888. int bitem_qty = sqlite3_column_int(statement, 1);
  1889. char *name = (char*)sqlite3_column_text(statement, 2);
  1890. if(name==nil)
  1891. name="";
  1892. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  1893. char *description = (char*)sqlite3_column_text(statement, 3);
  1894. if(description==nil)
  1895. description="";
  1896. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  1897. double unit_price = sqlite3_column_double(statement, 4);
  1898. int use_unitprice = sqlite3_column_int(statement, 5);
  1899. if(use_unitprice!=1)
  1900. {
  1901. unit_price=[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db].doubleValue;
  1902. }
  1903. itemjson[@"model"]=nsname;
  1904. itemjson[@"description"]=nsdescription;
  1905. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  1906. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  1907. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  1908. if(compute)
  1909. {
  1910. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute];
  1911. }
  1912. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1913. count++;
  1914. }
  1915. sqlite3_finalize(statement);
  1916. }
  1917. ret[@"count"]=@(count);
  1918. if(count==0)
  1919. return nil;
  1920. else
  1921. return ret;
  1922. }
  1923. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  1924. {
  1925. //compute: add part to subtotal;
  1926. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  1927. dict_item[@(item_id)]=@"1";
  1928. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1929. double cuft=0;
  1930. double weight=0;
  1931. int carton=0;
  1932. 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];
  1933. sqlite3_stmt * statement;
  1934. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1935. {
  1936. if (sqlite3_step(statement) == SQLITE_ROW)
  1937. {
  1938. double ulength = sqlite3_column_double(statement, 0);
  1939. double uwidth = sqlite3_column_double(statement, 1);
  1940. double uheight = sqlite3_column_double(statement, 2);
  1941. double uweight = sqlite3_column_double(statement, 3);
  1942. double mlength = sqlite3_column_double(statement, 4);
  1943. double mwidth = sqlite3_column_double(statement, 5);
  1944. double mheight = sqlite3_column_double(statement, 6);
  1945. double mweight = sqlite3_column_double(statement, 7);
  1946. double ilength = sqlite3_column_double(statement, 8);
  1947. double iwidth = sqlite3_column_double(statement, 9);
  1948. double iheight = sqlite3_column_double(statement, 10);
  1949. double iweight = sqlite3_column_double(statement, 11);
  1950. int pcs = sqlite3_column_int(statement,12);
  1951. int mpack = sqlite3_column_int(statement, 13);
  1952. int ipack = sqlite3_column_int(statement, 14);
  1953. double ucbf = sqlite3_column_double(statement, 15);
  1954. double icbf = sqlite3_column_double(statement, 16);
  1955. double mcbf = sqlite3_column_double(statement, 17);
  1956. if(ipack==0)
  1957. {
  1958. carton= count/mpack ;
  1959. weight = mweight*carton;
  1960. cuft= carton*(mlength*mwidth*mheight);
  1961. int remain=count%mpack;
  1962. if(remain==0)
  1963. {
  1964. //do nothing;
  1965. }
  1966. else
  1967. {
  1968. carton++;
  1969. weight += uweight*remain;
  1970. cuft += (ulength*uwidth*uheight)*remain;
  1971. }
  1972. }
  1973. else
  1974. {
  1975. carton = count/(mpack*ipack);
  1976. weight = mweight*carton;
  1977. cuft= carton*(mlength*mwidth*mheight);
  1978. int remain=count%(mpack*ipack);
  1979. if(remain==0)
  1980. {
  1981. // do nothing;
  1982. }
  1983. else
  1984. {
  1985. carton++;
  1986. int icarton =remain/ipack;
  1987. int iremain=remain%ipack;
  1988. weight += iweight*icarton;
  1989. cuft += (ilength*iwidth*iheight)*icarton;
  1990. if(iremain==0)
  1991. {
  1992. //do nothing;
  1993. }
  1994. else
  1995. {
  1996. weight += uweight*iremain;
  1997. cuft += (ulength*uwidth*uheight)*iremain;
  1998. }
  1999. }
  2000. }
  2001. #ifdef BUILD_NPD
  2002. cuft=ucbf*count;
  2003. weight= uweight*count;
  2004. #endif
  2005. }
  2006. sqlite3_finalize(statement);
  2007. }
  2008. if(compute)
  2009. {
  2010. NSArray * arr_count=nil;
  2011. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  2012. for(int i=0;i<arr_bundle.count;i++)
  2013. {
  2014. dict_item[arr_bundle[i]]=@"1";
  2015. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  2016. cuft+=[bundlejson[@"cuft"] doubleValue];
  2017. weight+=[bundlejson[@"weight"] doubleValue];
  2018. carton+=[bundlejson[@"carton"] intValue];
  2019. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  2020. }
  2021. }
  2022. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  2023. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  2024. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  2025. ret[@"items"]=dict_item;
  2026. return ret;
  2027. }
  2028. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  2029. {
  2030. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  2031. // 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 ];
  2032. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2033. DebugLog(@"offline_login sql:%@",sqlQuery);
  2034. sqlite3_stmt * statement;
  2035. int cart_count=0;
  2036. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2037. if ( dbresult== SQLITE_OK)
  2038. {
  2039. while (sqlite3_step(statement) == SQLITE_ROW)
  2040. {
  2041. int item_id = sqlite3_column_int(statement, 0);
  2042. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  2043. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2044. }
  2045. sqlite3_finalize(statement);
  2046. }
  2047. return cart_count;
  2048. }
  2049. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  2050. {
  2051. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2052. sqlite3 *db = [iSalesDB get_db];
  2053. UIApplication * app = [UIApplication sharedApplication];
  2054. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2055. NSString* orderCode=params[@"orderCode"];
  2056. int sort = [[params objectForKey:@"sort"] intValue];
  2057. NSString *sort_str = @"";
  2058. switch (sort) {
  2059. case 0:{
  2060. sort_str = @"order by c.modify_time desc";
  2061. }
  2062. break;
  2063. case 1:{
  2064. sort_str = @"order by c.modify_time asc";
  2065. }
  2066. break;
  2067. case 2:{
  2068. sort_str = @"order by m.name asc";
  2069. }
  2070. break;
  2071. case 3:{
  2072. sort_str = @"order by m.name desc";
  2073. }
  2074. break;
  2075. case 4:{
  2076. sort_str = @"order by m.description asc";
  2077. }
  2078. break;
  2079. default:
  2080. break;
  2081. }
  2082. 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 ];
  2083. // 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 ];
  2084. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2085. DebugLog(@"offline_login sql:%@",sqlQuery);
  2086. sqlite3_stmt * statement;
  2087. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2088. NSDate *date1 = [NSDate date];
  2089. int count=0;
  2090. int cart_count=0;
  2091. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2092. if ( dbresult== SQLITE_OK)
  2093. {
  2094. while (sqlite3_step(statement) == SQLITE_ROW)
  2095. {
  2096. NSDate *row_date = [NSDate date];
  2097. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2098. int product_id = sqlite3_column_int(statement, 0);
  2099. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2100. int item_id = sqlite3_column_int(statement, 7);
  2101. NSString* Price=nil;
  2102. if(str_price==nil)
  2103. {
  2104. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2105. // NSDate *price_date = [NSDate date];
  2106. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2107. // DebugLog(@"price time interval");
  2108. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  2109. if(price==nil)
  2110. Price=@"No Price.";
  2111. else
  2112. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2113. }
  2114. else
  2115. {
  2116. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2117. }
  2118. double discount = sqlite3_column_double(statement, 2);
  2119. int item_count = sqlite3_column_int(statement, 3);
  2120. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2121. NSString *nsline_note=nil;
  2122. if(line_note!=nil)
  2123. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2124. char *name = (char*)sqlite3_column_text(statement, 5);
  2125. NSString *nsname=nil;
  2126. if(name!=nil)
  2127. nsname= [[NSString alloc]initWithUTF8String:name];
  2128. char *description = (char*)sqlite3_column_text(statement, 6);
  2129. NSString *nsdescription=nil;
  2130. if(description!=nil)
  2131. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2132. int stockUom = sqlite3_column_int(statement, 8);
  2133. int _id = sqlite3_column_int(statement, 9);
  2134. // NSDate *subtotal_date = [NSDate date];
  2135. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2136. // DebugLog(@"subtotal_date time interval");
  2137. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  2138. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2139. double weight=[bsubtotaljson[@"weight"] doubleValue];
  2140. int carton=[bsubtotaljson[@"carton"] intValue];
  2141. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2142. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2143. // NSDate *img_date = [NSDate date];
  2144. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2145. // DebugLog(@"img_date time interval");
  2146. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  2147. itemjson[@"model"]=nsname;
  2148. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2149. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2150. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2151. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2152. itemjson[@"check"]=@"true";
  2153. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2154. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2155. itemjson[@"unit_price"]=Price;
  2156. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2157. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2158. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2159. itemjson[@"note"]=nsline_note;
  2160. // NSDate *date2 = [NSDate date];
  2161. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  2162. // DebugLog(@"model_bundle time interval");
  2163. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  2164. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2165. count++;
  2166. // DebugLog(@"row time interval");
  2167. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  2168. }
  2169. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2170. sqlite3_finalize(statement);
  2171. }
  2172. DebugLog(@"request cart total time interval");
  2173. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2174. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2175. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2176. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2177. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2178. ret[@"count"]=[NSNumber numberWithInt:count ];
  2179. ret[@"mode"]=@"Regular Mode";
  2180. [iSalesDB close_db:db];
  2181. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  2182. DebugLog(@"general notes :%@",general_note);
  2183. ret[@"general_note"]= general_note;
  2184. return [RAUtils dict2data:ret];
  2185. }
  2186. +(NSData*) offline_login :(NSMutableDictionary *) params
  2187. {
  2188. NSString* user = [params valueForKey:@"user"];
  2189. NSString* password = [params valueForKey:@"password"];
  2190. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2191. sqlite3 *db = [iSalesDB get_db];
  2192. 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"]];
  2193. DebugLog(@"offline_login sql:%@",sqlQuery);
  2194. sqlite3_stmt * statement;
  2195. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2196. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2197. {
  2198. if (sqlite3_step(statement) == SQLITE_ROW)
  2199. {
  2200. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2201. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2202. int can_show_price = sqlite3_column_int(statement, 0);
  2203. int can_see_price = sqlite3_column_int(statement, 1);
  2204. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2205. if(contact_id==nil)
  2206. contact_id="";
  2207. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2208. int user_type = sqlite3_column_int(statement, 3);
  2209. int can_cancel_order = sqlite3_column_int(statement, 4);
  2210. int can_set_cart_price = sqlite3_column_int(statement, 5);
  2211. int can_create_portfolio = sqlite3_column_int(statement, 6);
  2212. int can_delete_order = sqlite3_column_int(statement, 7);
  2213. int can_submit_order = sqlite3_column_int(statement, 8);
  2214. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2215. int can_create_order = sqlite3_column_int(statement, 10);
  2216. char *mode = (char*)sqlite3_column_text(statement, 11);
  2217. if(mode==nil)
  2218. mode="";
  2219. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2220. char *username = (char*)sqlite3_column_text(statement, 12);
  2221. if(username==nil)
  2222. username="";
  2223. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2224. int can_update_contact_info = sqlite3_column_int(statement, 13);
  2225. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2226. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2227. [header setValue:nscontact_id forKey:@"contact_id"];
  2228. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2229. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2230. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2231. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2232. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2233. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2234. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2235. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2236. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  2237. [header setValue:nsusername forKey:@"username"];
  2238. NSError* error=nil;
  2239. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  2240. [header setValue:statusFilter forKey:@"statusFilter"];
  2241. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2242. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  2243. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  2244. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  2245. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"1=1"];
  2246. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  2247. [ret setObject:header forKey:@"header"];
  2248. [ret setValue:nsmode forKey:@"mode"];
  2249. }
  2250. sqlite3_finalize(statement);
  2251. }
  2252. [iSalesDB close_db:db];
  2253. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2254. return [RAUtils dict2data:ret];
  2255. }
  2256. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  2257. {
  2258. NSString* contactId = [params valueForKey:@"contactId"];
  2259. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2260. sqlite3 *db = [iSalesDB get_db];
  2261. NSString *sqlQuery = nil;
  2262. {
  2263. 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 from offline_contact where contact_id='%@'",contactId];
  2264. }
  2265. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  2266. sqlite3_stmt * statement;
  2267. [ret setValue:@"2" forKey:@"result"];
  2268. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2269. {
  2270. //int i = 0;
  2271. if (sqlite3_step(statement) == SQLITE_ROW)
  2272. {
  2273. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2274. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2275. // int editable = sqlite3_column_int(statement, 0);
  2276. char *company_name = (char*)sqlite3_column_text(statement, 1);
  2277. NSString *nscompany_name =nil;
  2278. if(company_name==nil)
  2279. nscompany_name=@"";
  2280. else
  2281. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  2282. char *country = (char*)sqlite3_column_text(statement, 2);
  2283. if(country==nil)
  2284. country="";
  2285. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  2286. // char *addr = (char*)sqlite3_column_text(statement, 3);
  2287. // if(addr==nil)
  2288. // addr="";
  2289. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  2290. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  2291. if(zipcode==nil)
  2292. zipcode="";
  2293. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  2294. char *state = (char*)sqlite3_column_text(statement, 5);
  2295. if(state==nil)
  2296. state="";
  2297. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  2298. char *city = (char*)sqlite3_column_text(statement, 6);
  2299. if(city==nil)
  2300. city="";
  2301. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  2302. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  2303. // NSString *nscontact_name = nil;
  2304. // if(contact_name==nil)
  2305. // nscontact_name=@"";
  2306. // else
  2307. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  2308. char *phone = (char*)sqlite3_column_text(statement, 8);
  2309. NSString *nsphone = nil;
  2310. if(phone==nil)
  2311. nsphone=@"";
  2312. else
  2313. nsphone= [[NSString alloc]initWithUTF8String:phone];
  2314. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  2315. if(contact_id==nil)
  2316. contact_id="";
  2317. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2318. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  2319. if(addr_1==nil)
  2320. addr_1="";
  2321. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  2322. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  2323. if(addr_2==nil)
  2324. addr_2="";
  2325. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  2326. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  2327. if(addr_3==nil)
  2328. addr_3="";
  2329. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  2330. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  2331. if(addr_4==nil)
  2332. addr_4="";
  2333. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  2334. char *first_name = (char*)sqlite3_column_text(statement, 14);
  2335. if(first_name==nil)
  2336. first_name="";
  2337. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  2338. char *last_name = (char*)sqlite3_column_text(statement, 15);
  2339. if(last_name==nil)
  2340. last_name="";
  2341. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  2342. char *fax = (char*)sqlite3_column_text(statement, 16);
  2343. NSString *nsfax = nil;
  2344. if(fax==nil)
  2345. nsfax=@"";
  2346. else
  2347. nsfax= [[NSString alloc]initWithUTF8String:fax];
  2348. char *email = (char*)sqlite3_column_text(statement, 17);
  2349. NSString *nsemail = nil;
  2350. if(email==nil)
  2351. nsemail=@"";
  2352. else
  2353. nsemail= [[NSString alloc]initWithUTF8String:email];
  2354. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  2355. NSString *nsimg_0 = nil;
  2356. if(img_0==nil)
  2357. nsimg_0=@"";
  2358. else
  2359. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2360. [self copy_bcardImg:nsimg_0];
  2361. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  2362. NSString *nsimg_1 = nil;
  2363. if(img_1==nil)
  2364. nsimg_1=@"";
  2365. else
  2366. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2367. [self copy_bcardImg:nsimg_1];
  2368. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  2369. NSString *nsimg_2 = nil;
  2370. if(img_2==nil)
  2371. nsimg_2=@"";
  2372. else
  2373. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2374. [self copy_bcardImg:nsimg_2];
  2375. char *price_type = (char*)sqlite3_column_text(statement, 21);
  2376. NSString *nsprice_type = nil;
  2377. if(price_type==nil)
  2378. nsprice_type=@"";
  2379. else
  2380. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  2381. char *notes = (char*)sqlite3_column_text(statement, 22);
  2382. NSString *nsnotes = nil;
  2383. if(notes==nil)
  2384. nsnotes=@"";
  2385. else
  2386. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  2387. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  2388. NSString *nssalesrep = nil;
  2389. if(salesrep==nil)
  2390. nssalesrep=@"";
  2391. else
  2392. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  2393. {
  2394. // decrypt
  2395. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  2396. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  2397. nsphone=[AESCrypt fastdecrypt:nsphone];
  2398. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2399. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  2400. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2401. }
  2402. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2403. [arr_name addObject:nsfirst_name];
  2404. [arr_name addObject:nslast_name];
  2405. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2406. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  2407. [arr_ext addObject:nsaddr_1];
  2408. [arr_ext addObject:nsaddr_2];
  2409. [arr_ext addObject:nsaddr_3];
  2410. [arr_ext addObject:nsaddr_4];
  2411. [arr_ext addObject:@"\r\n"];
  2412. [arr_ext addObject:nscity];
  2413. [arr_ext addObject:nsstate];
  2414. [arr_ext addObject:nszipcode];
  2415. [arr_ext addObject:nscountry];
  2416. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  2417. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  2418. [item setValue:nsimg_2 forKey:@"business_card_2"];
  2419. [item setValue:nsimg_0 forKey:@"business_card_0"];
  2420. [item setValue:nscountry forKey:@"customer_country"];
  2421. [item setValue:nsphone forKey:@"customer_phone"];
  2422. [item setValue:nsimg_1 forKey:@"business_card_1"];
  2423. [item setValue:nscompany_name forKey:@"customer_name"];
  2424. [item setValue:nsprice_type forKey:@"customer_price_type"];
  2425. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  2426. [item setValue:nsext forKey:@"customer_contact_ext"];
  2427. [item setValue:nszipcode forKey:@"customer_zipcode"];
  2428. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  2429. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  2430. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  2431. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  2432. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  2433. [item setValue:nslast_name forKey:@"customer_last_name"];
  2434. [item setValue:nscity forKey:@"customer_city"];
  2435. [item setValue:nsstate forKey:@"customer_state"];
  2436. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  2437. [item setValue:contactId forKey:@"customer_cid"];
  2438. [item setValue:nscontact_name forKey:@"customer_contact"];
  2439. [item setValue:nsfax forKey:@"customer_fax"];
  2440. [item setValue:nsemail forKey:@"customer_email"];
  2441. [ret setObject:item forKey:@"customerInfo"];
  2442. // i++;
  2443. }
  2444. UIApplication * app = [UIApplication sharedApplication];
  2445. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2446. [ret setValue:appDelegate.mode forKey:@"mode"];
  2447. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  2448. sqlite3_finalize(statement);
  2449. }
  2450. [iSalesDB close_db:db];
  2451. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2452. return ret;
  2453. }
  2454. + (bool) copy_bcardImg:(NSString*) filename
  2455. {
  2456. if(filename.length==0)
  2457. return false;
  2458. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2459. bool ret=false;
  2460. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2461. NSString *cachefolder = [paths objectAtIndex:0];
  2462. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2463. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2464. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2465. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2466. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  2467. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2468. //
  2469. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2470. NSFileManager* fileManager = [NSFileManager defaultManager];
  2471. BOOL bdir=NO;
  2472. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2473. {
  2474. NSError *error = nil;
  2475. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2476. {
  2477. ret=false;
  2478. }
  2479. else
  2480. {
  2481. ret=true;
  2482. }
  2483. // NSError *error = nil;
  2484. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2485. //
  2486. // if(!bsuccess)
  2487. // {
  2488. // DebugLog(@"Create offline_createimg folder failed");
  2489. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2490. // return [RAUtils dict2data:ret];
  2491. // }
  2492. // if(bsuccess)
  2493. // {
  2494. // sqlite3 *db = [self get_db];
  2495. //
  2496. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2497. // [iSalesDB close_db:db];
  2498. // }
  2499. }
  2500. return ret;
  2501. //
  2502. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2503. // if(bsuccess)
  2504. // {
  2505. // NSError *error = nil;
  2506. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2507. // {
  2508. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2509. // }
  2510. // else
  2511. // {
  2512. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2513. //
  2514. // ret[@"img_url_aname"]=filename;
  2515. // ret[@"img_url"]=savedImagePath;
  2516. // }
  2517. // }
  2518. }
  2519. +(NSData *) offline_saveBusinesscard:(NSData *) image
  2520. {
  2521. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2522. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2523. NSString *cachefolder = [paths objectAtIndex:0];
  2524. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2525. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2526. NSFileManager* fileManager = [NSFileManager defaultManager];
  2527. BOOL bdir=YES;
  2528. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  2529. {
  2530. NSError *error = nil;
  2531. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2532. if(!bsuccess)
  2533. {
  2534. DebugLog(@"Create offline_createimg folder failed");
  2535. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2536. return [RAUtils dict2data:ret];
  2537. }
  2538. // if(bsuccess)
  2539. // {
  2540. // sqlite3 *db = [self get_db];
  2541. //
  2542. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2543. // [iSalesDB close_db:db];
  2544. // }
  2545. }
  2546. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  2547. //JEPG格式
  2548. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  2549. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  2550. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2551. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2552. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2553. if(bsuccess)
  2554. {
  2555. NSError *error = nil;
  2556. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2557. {
  2558. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2559. }
  2560. else
  2561. {
  2562. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2563. ret[@"img_url_aname"]=filename;
  2564. ret[@"img_url"]=filename;
  2565. }
  2566. }
  2567. else
  2568. {
  2569. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2570. }
  2571. return [RAUtils dict2data:ret];
  2572. }
  2573. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  2574. {
  2575. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  2576. category = [category substringToIndex:3];
  2577. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2578. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  2579. params[@"category"]= category;
  2580. ret[@"params"]= params;
  2581. [ret setValue:@"detail" forKey:@"target"];
  2582. [ret setValue:@"popup" forKey:@"action"];
  2583. [ret setValue:@"content" forKey:@"type"];
  2584. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  2585. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2586. [ret setValue:@"true" forKey:@"single_row"];
  2587. [ret setValue:@"true" forKey:@"partial_refresh"];
  2588. // sqlite3 *db = [iSalesDB get_db];
  2589. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2590. 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 ;
  2591. sqlite3_stmt * statement;
  2592. int count = 0;
  2593. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  2594. // int count=0;
  2595. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2596. {
  2597. int i=0;
  2598. while (sqlite3_step(statement) == SQLITE_ROW)
  2599. {
  2600. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2601. // char *name = (char*)sqlite3_column_text(statement, 1);
  2602. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2603. char *name = (char*)sqlite3_column_text(statement, 0);
  2604. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2605. int product_id = sqlite3_column_int(statement, 1);
  2606. char *url = (char*)sqlite3_column_text(statement, 2);
  2607. if(url==nil)
  2608. url="";
  2609. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2610. [item setValue:nsurl forKey:@"picture_path"];
  2611. [item setValue:nsname forKey:@"fash_name"];
  2612. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2613. [item setValue:category forKey:@"category"];
  2614. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2615. i++;
  2616. }
  2617. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2618. sqlite3_finalize(statement);
  2619. }
  2620. NSLog(@"count:%d",count);
  2621. // [iSalesDB close_db:db];
  2622. return ret;
  2623. }
  2624. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  2625. {
  2626. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2627. [ret setValue:key forKey:@"key"];
  2628. [ret setValue:value forKey:@"val"];
  2629. return ret;
  2630. }
  2631. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  2632. {
  2633. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2634. [ret setValue:@"0" forKey:@"img_count"];
  2635. // sqlite3 *db = [iSalesDB get_db];
  2636. 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 ;
  2637. sqlite3_stmt * statement;
  2638. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2639. {
  2640. int i=0;
  2641. if (sqlite3_step(statement) == SQLITE_ROW)
  2642. {
  2643. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2644. // char *name = (char*)sqlite3_column_text(statement, 1);
  2645. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2646. char *value = (char*)sqlite3_column_text(statement, 0);
  2647. if(value==nil)
  2648. value="";
  2649. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2650. char *key = (char*)sqlite3_column_text(statement, 1);
  2651. if(key==nil)
  2652. key="";
  2653. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  2654. [item setValue:nsvalue forKey:@"val"];
  2655. [item setValue:nskey forKey:@"key"];
  2656. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2657. [ret setValue:@"1" forKey:@"count"];
  2658. i++;
  2659. }
  2660. sqlite3_finalize(statement);
  2661. }
  2662. // [iSalesDB close_db:db];
  2663. return ret;
  2664. }
  2665. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  2666. {
  2667. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2668. [ret setValue:@"0" forKey:@"count"];
  2669. // sqlite3 *db = [iSalesDB get_db];
  2670. 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;
  2671. sqlite3_stmt * statement;
  2672. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2673. {
  2674. int i=0;
  2675. while (sqlite3_step(statement) == SQLITE_ROW)
  2676. {
  2677. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2678. // char *name = (char*)sqlite3_column_text(statement, 1);
  2679. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2680. char *value = (char*)sqlite3_column_text(statement, 0);
  2681. if(value==nil)
  2682. value="";
  2683. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2684. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  2685. if(selector_display==nil)
  2686. selector_display="";
  2687. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  2688. int product_id = sqlite3_column_int(statement, 2);
  2689. char *category = (char*)sqlite3_column_text(statement, 3);
  2690. if(category==nil)
  2691. category="";
  2692. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  2693. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  2694. [item setValue:nsvalue forKey:@"title"];
  2695. [item setValue:url forKey:@"pic_url"];
  2696. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  2697. [params setValue:@"2" forKey:@"count"];
  2698. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  2699. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  2700. [param0 setValue:@"product_id" forKey:@"name"];
  2701. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  2702. [param1 setValue:nscategory forKey:@"val"];
  2703. [param1 setValue:@"category" forKey:@"name"];
  2704. [params setObject:param0 forKey:@"param_0"];
  2705. [params setObject:param1 forKey:@"param_1"];
  2706. [item setObject:params forKey:@"params"];
  2707. [ret setValue:nsselector_display forKey:@"name"];
  2708. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2709. i++;
  2710. }
  2711. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2712. [ret setValue:@"switch" forKey:@"action"];
  2713. sqlite3_finalize(statement);
  2714. }
  2715. // [iSalesDB close_db:db];
  2716. return ret;
  2717. }
  2718. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  2719. {
  2720. // model 在 category search 显示的图片。
  2721. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  2722. // sqlite3 *db = [iSalesDB get_db];
  2723. 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];
  2724. sqlite3_stmt * statement;
  2725. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2726. {
  2727. while (sqlite3_step(statement) == SQLITE_ROW)
  2728. {
  2729. char *url = (char*)sqlite3_column_text(statement, 0);
  2730. if(url==nil)
  2731. url="";
  2732. int type = sqlite3_column_int(statement, 1);
  2733. if(type==0)
  2734. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  2735. else
  2736. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  2737. }
  2738. sqlite3_finalize(statement);
  2739. }
  2740. // [iSalesDB close_db:db];
  2741. return ret;
  2742. }
  2743. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  2744. {
  2745. int item_id=-1;
  2746. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  2747. sqlite3_stmt * statement;
  2748. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2749. {
  2750. if (sqlite3_step(statement) == SQLITE_ROW)
  2751. {
  2752. item_id = sqlite3_column_int(statement, 0);
  2753. }
  2754. sqlite3_finalize(statement);
  2755. }
  2756. return item_id;
  2757. }
  2758. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  2759. {
  2760. // NSString* ret = @"";
  2761. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  2762. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  2763. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  2764. sqlite3_stmt * statement;
  2765. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2766. {
  2767. while (sqlite3_step(statement) == SQLITE_ROW)
  2768. {
  2769. int bitem_id = sqlite3_column_int(statement, 0);
  2770. int bitem_qty = sqlite3_column_int(statement, 1);
  2771. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  2772. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  2773. }
  2774. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  2775. sqlite3_finalize(statement);
  2776. }
  2777. // if(ret==nil)
  2778. // ret=@"";
  2779. *count=arr_count;
  2780. return arr_bundle;
  2781. }
  2782. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  2783. {
  2784. // get default sold qty, return -1 if model not found;
  2785. int ret = -1;
  2786. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  2787. sqlite3_stmt * statement;
  2788. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2789. {
  2790. if (sqlite3_step(statement) == SQLITE_ROW)
  2791. {
  2792. ret = sqlite3_column_int(statement, 0);
  2793. }
  2794. sqlite3_finalize(statement);
  2795. }
  2796. return ret;
  2797. }
  2798. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  2799. {
  2800. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2801. [ret setValue:@"0" forKey:@"img_count"];
  2802. // sqlite3 *db = [iSalesDB get_db];
  2803. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  2804. sqlite3_stmt * statement;
  2805. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2806. {
  2807. int i=0;
  2808. while (sqlite3_step(statement) == SQLITE_ROW)
  2809. {
  2810. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2811. // char *name = (char*)sqlite3_column_text(statement, 1);
  2812. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2813. char *url = (char*)sqlite3_column_text(statement, 0);
  2814. if(url==nil)
  2815. url="";
  2816. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2817. [item setValue:nsurl forKey:@"s"];
  2818. [item setValue:nsurl forKey:@"l"];
  2819. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  2820. i++;
  2821. }
  2822. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  2823. sqlite3_finalize(statement);
  2824. }
  2825. // [iSalesDB close_db:db];
  2826. return ret;
  2827. }
  2828. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  2829. UIApplication * app = [UIApplication sharedApplication];
  2830. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2831. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2832. sqlite3 *db = [iSalesDB get_db];
  2833. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2834. NSString* product_id=params[@"product_id"];
  2835. NSString *item_count_str = params[@"item_count"];
  2836. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2837. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  2838. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2839. // NSString *sql = @"";
  2840. for(int i=0;i<arr.count;i++)
  2841. {
  2842. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2843. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2844. __block int cart_count = 0;
  2845. if (!item_count_str) {
  2846. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  2847. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2848. NSString *model_set = [self textAtColumn:0 statement:stmt];
  2849. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  2850. cart_count = [[model_set_components lastObject] integerValue];
  2851. }];
  2852. }
  2853. if(count==0)
  2854. {
  2855. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2856. sqlite3_stmt *stmt;
  2857. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  2858. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  2859. if (item_count_arr) {
  2860. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  2861. } else {
  2862. sqlite3_bind_int(stmt,2,cart_count);
  2863. }
  2864. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2865. [iSalesDB execSql:@"ROLLBACK" db:db];
  2866. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2867. [iSalesDB close_db:db];
  2868. DebugLog(@"add to wishlist error");
  2869. return ret;
  2870. }
  2871. } else {
  2872. int qty = 0;
  2873. if (item_count_arr) {
  2874. qty = [item_count_arr[i] integerValue];
  2875. } else {
  2876. qty = cart_count;
  2877. }
  2878. 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]];
  2879. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  2880. [iSalesDB execSql:@"ROLLBACK" db:db];
  2881. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  2882. [iSalesDB close_db:db];
  2883. DebugLog(@"add to wishlist error");
  2884. return ret;
  2885. }
  2886. }
  2887. }
  2888. // [iSalesDB execSql:sql db:db];
  2889. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2890. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2891. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2892. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  2893. [iSalesDB close_db:db];
  2894. appDelegate.wish_count =count;
  2895. [appDelegate update_count_mark];
  2896. ret[@"result"]= [NSNumber numberWithInt:2];
  2897. return ret;
  2898. }
  2899. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  2900. // 0 category
  2901. // 1 search
  2902. // 2 itemsearch
  2903. NSData *ret = nil;
  2904. NSDictionary *items = nil;
  2905. switch (from) {
  2906. case 0:{
  2907. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  2908. }
  2909. break;
  2910. case 1:{
  2911. items = [[self search:params limited:NO] objectForKey:@"items"];
  2912. }
  2913. break;
  2914. case 2:{
  2915. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  2916. }
  2917. break;
  2918. default:
  2919. break;
  2920. }
  2921. if (!items) {
  2922. return ret;
  2923. }
  2924. int count = [[items objectForKey:@"count"] intValue];
  2925. NSMutableString *product_id_str = [@"" mutableCopy];
  2926. for (int i = 0; i < count; i++) {
  2927. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  2928. NSDictionary *item = [items objectForKey:key];
  2929. NSString *product_id = [item objectForKey:@"product_id"];
  2930. if (i == 0) {
  2931. [product_id_str appendString:product_id];
  2932. } else {
  2933. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  2934. }
  2935. }
  2936. NSString *add_to = [self valueInParams:params key:@"addTo"];
  2937. if ([add_to isEqualToString:@"cart"]) {
  2938. NSString *order_code = [params objectForKey:@"orderCode"];
  2939. if (order_code.length) {
  2940. NSDictionary *newParams = @{
  2941. @"product_id" : product_id_str,
  2942. @"orderCode" : order_code
  2943. };
  2944. ret = [self offline_add2cart:newParams.mutableCopy];
  2945. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2946. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2947. ret = [RAUtils dict2data:retDic];
  2948. }
  2949. } else if([add_to isEqualToString:@"wishlist"]) {
  2950. NSDictionary *newParams = @{
  2951. @"product_id" : product_id_str
  2952. };
  2953. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  2954. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2955. ret = [RAUtils dict2data:retDic];
  2956. } else if([add_to isEqualToString:@"portfolio"]) {
  2957. }
  2958. return ret;
  2959. }
  2960. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  2961. {
  2962. return [self addAll:params from:0];
  2963. }
  2964. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  2965. {
  2966. return [self addAll:params from:1];
  2967. }
  2968. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  2969. {
  2970. return [self addAll:params from:2];
  2971. }
  2972. #pragma mark - Jack
  2973. #warning 做SQL操作时转义!!
  2974. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  2975. // "val_227" : {
  2976. // "check" : 1,
  2977. // "value" : "US United States",
  2978. // "value_id" : "228"
  2979. // },
  2980. if (!countryCode) {
  2981. countryCode = @"US";
  2982. }
  2983. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  2984. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  2985. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  2986. int code_id = sqlite3_column_int(stmt, 3); // id
  2987. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2988. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  2989. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  2990. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2991. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  2992. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2993. }
  2994. long n = *count;
  2995. *count = n + 1;
  2996. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  2997. [container setValue:countryDic forKey:key];
  2998. }] mutableCopy];
  2999. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3000. return ret;
  3001. }
  3002. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  3003. countryCode = [self translateSingleQuote:countryCode];
  3004. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  3005. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3006. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  3007. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  3008. if (name == NULL) {
  3009. name = "";
  3010. }
  3011. if (code == NULL) {
  3012. code = "";
  3013. }
  3014. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3015. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3016. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3017. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3018. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  3019. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3020. }
  3021. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3022. [container setValue:stateDic forKey:key];
  3023. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  3024. DebugLog(@"query all state error: %@",err_msg);
  3025. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3026. // [stateDic setValue:@"Other" forKey:@"value"];
  3027. // [stateDic setValue:@"" forKey:@"value_id"];
  3028. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3029. //
  3030. // if (state_code && [@"" isEqualToString:state_code]) {
  3031. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3032. // }
  3033. //
  3034. // NSString *key = [NSString stringWithFormat:@"val_0"];
  3035. // [container setValue:stateDic forKey:key];
  3036. }] mutableCopy];
  3037. // failure 可以不用了,一样的
  3038. if (ret.allKeys.count == 0) {
  3039. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3040. [stateDic setValue:@"Other" forKey:@"value"];
  3041. [stateDic setValue:@"" forKey:@"value_id"];
  3042. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3043. if (state_code && [@"" isEqualToString:state_code]) {
  3044. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3045. }
  3046. NSString *key = [NSString stringWithFormat:@"val_0"];
  3047. [ret setValue:stateDic forKey:key];
  3048. }
  3049. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3050. return ret;
  3051. }
  3052. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  3053. codeId = [self translateSingleQuote:codeId];
  3054. 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];
  3055. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3056. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  3057. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  3058. if (name == NULL) {
  3059. name = "";
  3060. }
  3061. if (code == NULL) {
  3062. code = "";
  3063. }
  3064. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3065. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3066. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3067. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3068. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  3069. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3070. }
  3071. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3072. [container setValue:stateDic forKey:key];
  3073. }] mutableCopy];
  3074. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3075. return ret;
  3076. }
  3077. + (NSDictionary *)offline_getPrice {
  3078. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  3079. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3080. char *name = (char *) sqlite3_column_text(stmt, 1);
  3081. int type = sqlite3_column_int(stmt, 2);
  3082. int orderBy = sqlite3_column_int(stmt, 3);
  3083. if (name == NULL) {
  3084. name = "";
  3085. }
  3086. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  3087. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3088. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  3089. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  3090. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3091. if (orderBy == 0) {
  3092. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3093. }
  3094. [container setValue:priceDic forKey:key];
  3095. }] mutableCopy];
  3096. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3097. return ret;
  3098. }
  3099. + (NSDictionary *)offline_getSalesRep {
  3100. // 首先从offline_login表中取出sales_code
  3101. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3102. NSString *user = app.user;
  3103. user = [self translateSingleQuote:user];
  3104. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  3105. __block NSString *user_code = @"";
  3106. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3107. char *code = (char *)sqlite3_column_text(stmt, 0);
  3108. if (code == NULL) {
  3109. code = "";
  3110. }
  3111. user_code = [NSString stringWithUTF8String:code];
  3112. }];
  3113. // 再取所有salesRep
  3114. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  3115. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3116. // 1 name 2 code 3 salesrep_id
  3117. char *name = (char *)sqlite3_column_text(stmt, 1);
  3118. char *code = (char *)sqlite3_column_text(stmt, 2);
  3119. int salesrep_id = sqlite3_column_int(stmt, 3);
  3120. if (name == NULL) {
  3121. name = "";
  3122. }
  3123. if (code == NULL) {
  3124. code = "";
  3125. }
  3126. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  3127. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  3128. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  3129. // 比较code 相等则check
  3130. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  3131. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3132. }
  3133. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  3134. }] mutableCopy];
  3135. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3136. return ret;
  3137. }
  3138. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  3139. zipcode = [self translateSingleQuote:zipcode];
  3140. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  3141. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3142. char *country = (char *)sqlite3_column_text(stmt, 0);
  3143. char *state = (char *)sqlite3_column_text(stmt, 1);
  3144. char *city = (char *)sqlite3_column_text(stmt, 2);
  3145. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  3146. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  3147. if (country == NULL) {
  3148. country = "";
  3149. }
  3150. if (state == NULL) {
  3151. state = "";
  3152. }
  3153. if (city == NULL) {
  3154. city = "";
  3155. }
  3156. if (country_code == NULL) {
  3157. country_code = "";
  3158. }
  3159. if (state_code == NULL) {
  3160. state_code = "";
  3161. }
  3162. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  3163. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  3164. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  3165. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  3166. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  3167. }] mutableCopy];
  3168. return ret;
  3169. }
  3170. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  3171. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  3172. [item setValue:value forKey:valueKey];
  3173. [dic setValue:item forKey:itemKey];
  3174. }
  3175. + (NSString *)countryCodeByid:(NSString *)code_id {
  3176. NSString *ret = nil;
  3177. code_id = [self translateSingleQuote:code_id];
  3178. sqlite3 *db = [iSalesDB get_db];
  3179. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  3180. sqlite3_stmt * statement;
  3181. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3182. while (sqlite3_step(statement) == SQLITE_ROW) {
  3183. char *code = (char *)sqlite3_column_text(statement, 0);
  3184. if (code == NULL) {
  3185. code = "";
  3186. }
  3187. ret = [NSString stringWithUTF8String:code];
  3188. }
  3189. sqlite3_finalize(statement);
  3190. }
  3191. [iSalesDB close_db:db];
  3192. return ret;
  3193. }
  3194. + (NSString *)countryCodeIdByCode:(NSString *)code {
  3195. NSString *ret = nil;
  3196. code = [self translateSingleQuote:code];
  3197. sqlite3 *db = [iSalesDB get_db];
  3198. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  3199. sqlite3_stmt * statement;
  3200. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3201. while (sqlite3_step(statement) == SQLITE_ROW) {
  3202. char *_id = (char *)sqlite3_column_text(statement, 0);
  3203. if (_id == NULL) {
  3204. _id = "";
  3205. }
  3206. ret = [NSString stringWithFormat:@"%s",_id];
  3207. }
  3208. sqlite3_finalize(statement);
  3209. }
  3210. [iSalesDB close_db:db];
  3211. return ret;
  3212. }
  3213. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  3214. NSString *name = nil;
  3215. codeId = [self translateSingleQuote:codeId];
  3216. sqlite3 *db = [iSalesDB get_db];
  3217. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  3218. sqlite3_stmt * statement;
  3219. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3220. while (sqlite3_step(statement) == SQLITE_ROW) {
  3221. char *value = (char *)sqlite3_column_text(statement, 0);
  3222. if (value == NULL) {
  3223. value = "";
  3224. }
  3225. name = [NSString stringWithUTF8String:value];
  3226. }
  3227. sqlite3_finalize(statement);
  3228. }
  3229. [iSalesDB close_db:db];
  3230. return name;
  3231. }
  3232. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  3233. NSString *ret = nil;
  3234. sqlite3 *db = [iSalesDB get_db];
  3235. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  3236. sqlite3_stmt * statement;
  3237. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3238. while (sqlite3_step(statement) == SQLITE_ROW) {
  3239. char *name = (char *)sqlite3_column_text(statement, 0);
  3240. if (name == NULL) {
  3241. name = "";
  3242. }
  3243. ret = [NSString stringWithUTF8String:name];
  3244. }
  3245. sqlite3_finalize(statement);
  3246. }
  3247. [iSalesDB close_db:db];
  3248. return ret;
  3249. }
  3250. + (NSString *)salesRepCodeById:(NSString *)_id {
  3251. NSString *ret = nil;
  3252. _id = [self translateSingleQuote:_id];
  3253. sqlite3 *db = [iSalesDB get_db];
  3254. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  3255. sqlite3_stmt * statement;
  3256. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3257. while (sqlite3_step(statement) == SQLITE_ROW) {
  3258. char *rep = (char *)sqlite3_column_text(statement, 0);
  3259. if (rep == NULL) {
  3260. rep = "";
  3261. }
  3262. ret = [NSString stringWithUTF8String:rep];
  3263. }
  3264. sqlite3_finalize(statement);
  3265. }
  3266. [iSalesDB close_db:db];
  3267. return ret;
  3268. }
  3269. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  3270. char *tx = (char *)sqlite3_column_text(stmt, col);
  3271. if (tx == NULL) {
  3272. tx = "";
  3273. }
  3274. NSString *text = [NSString stringWithFormat:@"%s",tx];
  3275. if (!text) {
  3276. text = @"";
  3277. }
  3278. // 将字符全部为' '的字符串干掉
  3279. int spaceCount = 0;
  3280. for (int i = 0; i < text.length; i++) {
  3281. if ([text characterAtIndex:i] == ' ') {
  3282. spaceCount++;
  3283. }
  3284. }
  3285. if (spaceCount == text.length) {
  3286. text = @"";
  3287. }
  3288. return text;
  3289. }
  3290. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  3291. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3292. NSData *data = [NSData dataWithContentsOfFile:path];
  3293. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3294. return ret;
  3295. }
  3296. + (NSString *)textFileName:(NSString *)name {
  3297. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3298. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  3299. if (!text) {
  3300. text = @"";
  3301. }
  3302. return text;
  3303. }
  3304. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  3305. return [[dic objectForKey:key] mutableCopy];
  3306. }
  3307. + (id)translateSingleQuote:(NSString *)string {
  3308. if ([string isKindOfClass:[NSString class]])
  3309. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3310. return string;
  3311. }
  3312. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  3313. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  3314. }
  3315. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  3316. NSString* ret= nil;
  3317. NSString *sqlQuery = nil;
  3318. // 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
  3319. 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];
  3320. sqlite3_stmt * statement;
  3321. // int count=0;
  3322. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3323. {
  3324. if (sqlite3_step(statement) == SQLITE_ROW)
  3325. {
  3326. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3327. if(imgurl==nil)
  3328. imgurl="";
  3329. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3330. ret=nsimgurl;
  3331. }
  3332. sqlite3_finalize(statement);
  3333. }
  3334. else
  3335. {
  3336. [ret setValue:@"8" forKey:@"result"];
  3337. }
  3338. // [iSalesDB close_db:db];
  3339. // DebugLog(@"data string: %@",ret );
  3340. return ret;
  3341. }
  3342. #pragma mark contact list
  3343. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  3344. {
  3345. // contactType = "Sales_Order_Customer";
  3346. // limit = 25;
  3347. // offset = 0;
  3348. // password = 123456;
  3349. // "price_name" = 16;
  3350. // user = EvanK;
  3351. sqlite3 *db = [iSalesDB get_db];
  3352. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  3353. if (contactType) {
  3354. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  3355. } else {
  3356. contactType = @"1 = 1";
  3357. }
  3358. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  3359. DebugLog(@"offline contact list keyword: %@",keyword);
  3360. // advanced search
  3361. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  3362. if (contact_name) {
  3363. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3364. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  3365. } else {
  3366. contact_name = @"";
  3367. }
  3368. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  3369. if (customer_phone) {
  3370. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3371. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  3372. } else {
  3373. customer_phone = @"";
  3374. }
  3375. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  3376. if (customer_fax) {
  3377. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3378. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  3379. } else {
  3380. customer_fax = @"";
  3381. }
  3382. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  3383. if (customer_zipcode) {
  3384. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3385. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  3386. } else {
  3387. customer_zipcode = @"";
  3388. }
  3389. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  3390. if (customer_sales_rep) {
  3391. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3392. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  3393. } else {
  3394. customer_sales_rep = @"";
  3395. }
  3396. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  3397. if (customer_state) {
  3398. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3399. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  3400. } else {
  3401. customer_state = @"";
  3402. }
  3403. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  3404. if (customer_name) {
  3405. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3406. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  3407. } else {
  3408. customer_name = @"";
  3409. }
  3410. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  3411. if (customer_country) {
  3412. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3413. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  3414. } else {
  3415. customer_country = @"";
  3416. }
  3417. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  3418. if (customer_cid) {
  3419. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3420. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  3421. } else {
  3422. customer_cid = @"";
  3423. }
  3424. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  3425. if (customer_city) {
  3426. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3427. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  3428. } else {
  3429. customer_city = @"";
  3430. }
  3431. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  3432. if (customer_address) {
  3433. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3434. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  3435. } else {
  3436. customer_address = @"";
  3437. }
  3438. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  3439. if (customer_email) {
  3440. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3441. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  3442. } else {
  3443. customer_email = @"";
  3444. }
  3445. NSString *price_name = [params valueForKey:@"price_name"];
  3446. if (price_name) {
  3447. if ([price_name containsString:@","]) {
  3448. // 首先从 price表中查处name
  3449. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  3450. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  3451. for (int i = 1;i < pArray.count;i++) {
  3452. NSString *p = pArray[i];
  3453. [mutablePStr appendFormat:@" or type = %@ ",p];
  3454. }
  3455. [mutablePStr appendString:@";"];
  3456. __block NSMutableArray *price_name_array = [NSMutableArray array];
  3457. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3458. char *name = (char *)sqlite3_column_text(stmt, 0);
  3459. if (!name)
  3460. name = "";
  3461. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  3462. }];
  3463. // 再根据name 拼sql
  3464. NSMutableString *mutable_price_name = [NSMutableString string];
  3465. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  3466. for (int i = 1; i < price_name_array.count; i++) {
  3467. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  3468. }
  3469. [mutable_price_name appendString:@")"];
  3470. price_name = mutable_price_name;
  3471. } else {
  3472. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  3473. if ([price_name isEqualToString:@""]) {
  3474. price_name = @"";
  3475. } else {
  3476. __block NSString *price;
  3477. price_name = [self translateSingleQuote:price_name];
  3478. [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) {
  3479. char *p = (char *)sqlite3_column_text(stmt, 0);
  3480. if (p == NULL) {
  3481. p = "";
  3482. }
  3483. price = [NSString stringWithUTF8String:p];
  3484. }];
  3485. if ([price isEqualToString:@""]) {
  3486. price_name = @"";
  3487. } else {
  3488. price = [self translateSingleQuote:price];
  3489. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  3490. }
  3491. }
  3492. }
  3493. } else {
  3494. price_name = @"";
  3495. }
  3496. int limit = [[params valueForKey:@"limit"] intValue];
  3497. int offset = [[params valueForKey:@"offset"] intValue];
  3498. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3499. 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];
  3500. 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];
  3501. // int result= [iSalesDB AddExFunction:db];
  3502. int count =0;
  3503. NSString *sqlQuery = nil;
  3504. if(keyword.length==0)
  3505. {
  3506. // 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];
  3507. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  3508. sqlQuery = sql;
  3509. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  3510. }
  3511. else
  3512. {
  3513. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  3514. keyword = keyword.lowercaseString;
  3515. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3516. 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];
  3517. 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]];
  3518. }
  3519. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3520. sqlite3_stmt * statement;
  3521. [ret setValue:@"2" forKey:@"result"];
  3522. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  3523. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3524. {
  3525. int i = 0;
  3526. while (sqlite3_step(statement) == SQLITE_ROW)
  3527. {
  3528. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3529. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3530. int editable = sqlite3_column_int(statement, 0);
  3531. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3532. NSString *nscompany_name =nil;
  3533. if(company_name==nil)
  3534. nscompany_name=@"";
  3535. else
  3536. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  3537. char *country = (char*)sqlite3_column_text(statement, 2);
  3538. if(country==nil)
  3539. country="";
  3540. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3541. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3542. // if(addr==nil)
  3543. // addr="";
  3544. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3545. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3546. if(zipcode==nil)
  3547. zipcode="";
  3548. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3549. char *state = (char*)sqlite3_column_text(statement, 5);
  3550. if(state==nil)
  3551. state="";
  3552. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3553. char *city = (char*)sqlite3_column_text(statement, 6);
  3554. if(city==nil)
  3555. city="";
  3556. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3557. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3558. // NSString *nscontact_name = nil;
  3559. // if(contact_name==nil)
  3560. // nscontact_name=@"";
  3561. // else
  3562. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3563. char *phone = (char*)sqlite3_column_text(statement, 8);
  3564. NSString *nsphone = nil;
  3565. if(phone==nil)
  3566. nsphone=@"";
  3567. else
  3568. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3569. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3570. if(contact_id==nil)
  3571. contact_id="";
  3572. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3573. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3574. if(addr_1==nil)
  3575. addr_1="";
  3576. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3577. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3578. if(addr_2==nil)
  3579. addr_2="";
  3580. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3581. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3582. if(addr_3==nil)
  3583. addr_3="";
  3584. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3585. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3586. if(addr_4==nil)
  3587. addr_4="";
  3588. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3589. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3590. if(first_name==nil)
  3591. first_name="";
  3592. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3593. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3594. if(last_name==nil)
  3595. last_name="";
  3596. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3597. char *fax = (char*)sqlite3_column_text(statement, 16);
  3598. NSString *nsfax = nil;
  3599. if(fax==nil)
  3600. nsfax=@"";
  3601. else
  3602. {
  3603. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3604. if(nsfax.length>0)
  3605. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  3606. }
  3607. char *email = (char*)sqlite3_column_text(statement, 17);
  3608. NSString *nsemail = nil;
  3609. if(email==nil)
  3610. nsemail=@"";
  3611. else
  3612. {
  3613. nsemail= [[NSString alloc]initWithUTF8String:email];
  3614. if(nsemail.length>0)
  3615. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  3616. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  3617. }
  3618. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3619. [arr_name addObject:nsfirst_name];
  3620. [arr_name addObject:nslast_name];
  3621. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3622. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  3623. {
  3624. // decrypt
  3625. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3626. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3627. nsphone=[AESCrypt fastdecrypt:nsphone];
  3628. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3629. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3630. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3631. }
  3632. [arr_addr addObject:nscompany_name];
  3633. [arr_addr addObject:nscontact_name];
  3634. [arr_addr addObject:@"<br>"];
  3635. [arr_addr addObject:nsaddr_1];
  3636. [arr_addr addObject:nsaddr_2];
  3637. [arr_addr addObject:nsaddr_3];
  3638. [arr_addr addObject:nsaddr_4];
  3639. //[arr_addr addObject:nsaddr];
  3640. [arr_addr addObject:nszipcode];
  3641. [arr_addr addObject:nscity];
  3642. [arr_addr addObject:nsstate];
  3643. [arr_addr addObject:nscountry];
  3644. [arr_addr addObject:@"<br>"];
  3645. [arr_addr addObject:nsphone];
  3646. [arr_addr addObject:nsfax];
  3647. [arr_addr addObject:nsemail];
  3648. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  3649. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  3650. [item setValue:name forKey:@"name"];
  3651. [item setValue:nscontact_id forKey:@"contact_id"];
  3652. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  3653. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3654. i++;
  3655. }
  3656. UIApplication * app = [UIApplication sharedApplication];
  3657. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3658. [ret setValue:appDelegate.mode forKey:@"mode"];
  3659. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  3660. sqlite3_finalize(statement);
  3661. }
  3662. [iSalesDB close_db:db];
  3663. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3664. return ret;
  3665. }
  3666. #pragma mark contact Advanced search
  3667. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  3668. {
  3669. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  3670. return [RAUtils dict2data:contactAdvanceDic];
  3671. }
  3672. #pragma mark create new contact
  3673. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  3674. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  3675. NSData *data = [NSData dataWithContentsOfFile:path];
  3676. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3677. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3678. NSString *countryCode = nil;
  3679. NSString *countryCode_id = nil;
  3680. NSString *stateCode = nil;
  3681. NSString *city = nil;
  3682. NSString *zipCode = nil;
  3683. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  3684. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  3685. NSString *code_id = params[@"country"];
  3686. countryCode_id = code_id;
  3687. countryCode = [self countryCodeByid:code_id];
  3688. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  3689. NSString *zip_code = params[@"zipcode"];
  3690. // 剔除全部为空格
  3691. int spaceCount = 0;
  3692. for (int i = 0; i < zip_code.length; i++) {
  3693. if ([zip_code characterAtIndex:i] == ' ') {
  3694. spaceCount++;
  3695. }
  3696. }
  3697. if (spaceCount == zip_code.length) {
  3698. zip_code = @"";
  3699. }
  3700. zipCode = zip_code;
  3701. if (zipCode.length > 0) {
  3702. countryCode_id = params[@"country"];
  3703. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  3704. countryCode = [dic valueForKey:@"country_code"];
  3705. if (!countryCode) {
  3706. countryCode = @"US";
  3707. }
  3708. stateCode = [dic valueForKey:@"state_code"];
  3709. city = [dic valueForKey:@"city"];
  3710. // zip code
  3711. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
  3712. [zipDic setValue:zipCode forKey:@"value"];
  3713. [section_0 setValue:zipDic forKey:@"item_10"];
  3714. }
  3715. }
  3716. } else {
  3717. // default: US United States
  3718. countryCode = @"US";
  3719. countryCode_id = @"228";
  3720. }
  3721. // country
  3722. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  3723. [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
  3724. // state
  3725. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  3726. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  3727. [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
  3728. // city
  3729. if (city) {
  3730. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
  3731. [cityDic setValue:city forKey:@"value"];
  3732. [section_0 setValue:cityDic forKey:@"item_12"];
  3733. }
  3734. // price type
  3735. NSDictionary *priceDic = [self offline_getPrice];
  3736. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  3737. // Sales Rep
  3738. NSDictionary *repDic = [self offline_getSalesRep];
  3739. [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  3740. [ret setValue:section_0 forKey:@"section_0"];
  3741. return [RAUtils dict2data:ret];
  3742. }
  3743. #pragma mark save
  3744. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  3745. NSString *addr = nil;
  3746. NSString *contact_name = nil;
  3747. NSString *companyName = [params objectForKey:@"company"];
  3748. if (companyName) {
  3749. companyName = [AESCrypt fastencrypt:companyName];
  3750. } else {
  3751. companyName = @"";
  3752. }
  3753. DebugLog(@"company");
  3754. companyName = [self translateSingleQuote:companyName];
  3755. NSString *addr1 = [params objectForKey:@"address"];
  3756. NSString *addr2 = [params objectForKey:@"address2"];
  3757. NSString *addr3 = [params objectForKey:@"address_3"];
  3758. NSString *addr4 = [params objectForKey:@"address_4"];
  3759. if (!addr2) {
  3760. addr2 = @"";
  3761. }
  3762. if (!addr3) {
  3763. addr3 = @"";
  3764. }
  3765. if (!addr4) {
  3766. addr4 = @"";
  3767. }
  3768. if (!addr1) {
  3769. addr1 = @"";
  3770. }
  3771. DebugLog(@"addr");
  3772. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  3773. addr = [AESCrypt fastencrypt:addr];
  3774. addr = [self translateSingleQuote:addr];
  3775. if (addr1 && ![addr1 isEqualToString:@""]) {
  3776. addr1 = [AESCrypt fastencrypt:addr1];
  3777. }
  3778. addr1 = [self translateSingleQuote:addr1];
  3779. addr2 = [self translateSingleQuote:addr2];
  3780. addr3 = [self translateSingleQuote:addr3];
  3781. addr4 = [self translateSingleQuote:addr4];
  3782. NSString *country = [params objectForKey:@"country"];
  3783. if (country) {
  3784. country = [self countryNameByCountryCodeId:country];
  3785. } else {
  3786. country = @"";
  3787. }
  3788. DebugLog(@"country");
  3789. country = [self translateSingleQuote:country];
  3790. NSString *state = [params objectForKey:@"state"];
  3791. if (!state) {
  3792. state = @"";
  3793. }
  3794. DebugLog(@"state");
  3795. state = [self translateSingleQuote:state];
  3796. NSString *city = [params objectForKey:@"city"];
  3797. if (!city) {
  3798. city = @"";
  3799. }
  3800. city = [self translateSingleQuote:city];
  3801. NSString *zipcode = [params objectForKey:@"zipcode"];
  3802. if (!zipcode) {
  3803. zipcode = @"";
  3804. }
  3805. DebugLog(@"zip");
  3806. zipcode = [self translateSingleQuote:zipcode];
  3807. NSString *fistName = [params objectForKey:@"firstname"];
  3808. if (!fistName) {
  3809. fistName = @"";
  3810. }
  3811. NSString *lastName = [params objectForKey:@"lastname"];
  3812. if (!lastName) {
  3813. lastName = @"";
  3814. }
  3815. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  3816. DebugLog(@"contact_name");
  3817. contact_name = [self translateSingleQuote:contact_name];
  3818. fistName = [self translateSingleQuote:fistName];
  3819. lastName = [self translateSingleQuote:lastName];
  3820. NSString *phone = [params objectForKey:@"phone"];
  3821. if (phone) {
  3822. phone = [AESCrypt fastencrypt:phone];
  3823. } else {
  3824. phone = @"";
  3825. }
  3826. DebugLog(@"PHONE");
  3827. phone = [self translateSingleQuote:phone];
  3828. NSString *fax = [params objectForKey:@"fax"];
  3829. if (!fax) {
  3830. fax = @"";
  3831. }
  3832. DebugLog(@"FAX");
  3833. fax = [self translateSingleQuote:fax];
  3834. NSString *email = [params objectForKey:@"email"];
  3835. if (!email) {
  3836. email = @"";
  3837. }
  3838. DebugLog(@"EMAIL:%@",email);
  3839. email = [self translateSingleQuote:email];
  3840. NSString *notes = [params objectForKey:@"contact_notes"];
  3841. if (!notes) {
  3842. notes = @"";
  3843. }
  3844. DebugLog(@"NOTE:%@",notes);
  3845. notes = [self translateSingleQuote:notes];
  3846. NSString *price = [params objectForKey:@"price_name"];
  3847. if (price) {
  3848. price = [self priceNameByPriceId:price];
  3849. } else {
  3850. price = @"";
  3851. }
  3852. DebugLog(@"PRICE");
  3853. price = [self translateSingleQuote:price];
  3854. NSString *salesRep = [params objectForKey:@"sales_rep"];
  3855. if (salesRep) {
  3856. salesRep = [self salesRepCodeById:salesRep];
  3857. } else {
  3858. salesRep = @"";
  3859. }
  3860. salesRep = [self translateSingleQuote:salesRep];
  3861. NSString *img = [params objectForKey:@"business_card"];
  3862. NSArray *array = [img componentsSeparatedByString:@","];
  3863. NSString *img_0 = array[0];
  3864. if (!img_0) {
  3865. img_0 = @"";
  3866. }
  3867. img_0 = [self translateSingleQuote:img_0];
  3868. NSString *img_1 = array[1];
  3869. if (!img_1) {
  3870. img_1 = @"";
  3871. }
  3872. img_1 = [self translateSingleQuote:img_1];
  3873. NSString *img_2 = array[2];
  3874. if (!img_2) {
  3875. img_2 = @"";
  3876. }
  3877. img_2 = [self translateSingleQuote:img_2];
  3878. NSString *contact_id = [NSUUID UUID].UUIDString;
  3879. // 判断更新时是否为customer
  3880. if (update) {
  3881. contact_id = [params objectForKey:@"contact_id"];
  3882. if (!contact_id) {
  3883. contact_id = @"";
  3884. }
  3885. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  3886. __block int customer = 0;
  3887. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3888. customer = sqlite3_column_int(stmt, 0);
  3889. }];
  3890. isCustomer = customer ? YES : NO;
  3891. }
  3892. NSMutableDictionary *sync_dic = [params mutableCopy];
  3893. if (isCustomer) {
  3894. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  3895. } else {
  3896. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  3897. }
  3898. NSString *sync_data = nil;
  3899. NSString *sql = nil;
  3900. if (update){
  3901. contact_id = [params objectForKey:@"contact_id"];
  3902. if (!contact_id) {
  3903. contact_id = @"";
  3904. }
  3905. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3906. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3907. sync_data = [RAUtils dict2string:sync_dic];
  3908. sync_data = [self translateSingleQuote:sync_data];
  3909. 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 = '%@' 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_id];
  3910. } else {
  3911. contact_id = [self translateSingleQuote:contact_id];
  3912. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3913. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3914. sync_data = [RAUtils dict2string:sync_dic];
  3915. sync_data = [self translateSingleQuote:sync_data];
  3916. 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) 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];
  3917. }
  3918. int result = [iSalesDB execSql:sql];
  3919. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  3920. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  3921. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  3922. }
  3923. #pragma mark save new contact
  3924. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  3925. {
  3926. return [self offline_saveContact:params update:NO isCustomer:YES];
  3927. }
  3928. #pragma mark edit contact
  3929. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  3930. {
  3931. // {
  3932. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  3933. // password = 123456;
  3934. // user = EvanK;
  3935. // }
  3936. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  3937. NSData *data = [NSData dataWithContentsOfFile:path];
  3938. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3939. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3940. NSString *countryCode = nil;
  3941. NSString *countryCode_id = nil;
  3942. NSString *stateCode = nil;
  3943. /*------contact infor------*/
  3944. __block NSString *country = nil;
  3945. __block NSString *company_name = nil;
  3946. __block NSString *contact_id = params[@"contact_id"];
  3947. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  3948. __block NSString *zipcode = nil;
  3949. __block NSString *state = nil; // state_code
  3950. __block NSString *city = nil; //
  3951. __block NSString *firt_name,*last_name;
  3952. __block NSString *phone,*fax,*email;
  3953. __block NSString *notes,*price_type,*sales_rep;
  3954. __block NSString *img_0,*img_1,*img_2;
  3955. contact_id = [self translateSingleQuote:contact_id];
  3956. 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 from offline_contact where contact_id = '%@';",contact_id];
  3957. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3958. country = [self textAtColumn:0 statement:stmt]; // country name
  3959. company_name = [self textAtColumn:1 statement:stmt];
  3960. addr_1 = [self textAtColumn:2 statement:stmt];
  3961. addr_2 = [self textAtColumn:3 statement:stmt];
  3962. addr_3 = [self textAtColumn:4 statement:stmt];
  3963. addr_4 = [self textAtColumn:5 statement:stmt];
  3964. zipcode = [self textAtColumn:6 statement:stmt];
  3965. state = [self textAtColumn:7 statement:stmt]; // state code
  3966. city = [self textAtColumn:8 statement:stmt];
  3967. firt_name = [self textAtColumn:9 statement:stmt];
  3968. last_name = [self textAtColumn:10 statement:stmt];
  3969. phone = [self textAtColumn:11 statement:stmt];
  3970. fax = [self textAtColumn:12 statement:stmt];
  3971. email = [self textAtColumn:13 statement:stmt];
  3972. notes = [self textAtColumn:14 statement:stmt];
  3973. price_type = [self textAtColumn:15 statement:stmt]; // name
  3974. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  3975. img_0 = [self textAtColumn:17 statement:stmt];
  3976. img_1 = [self textAtColumn:18 statement:stmt];
  3977. img_2 = [self textAtColumn:19 statement:stmt];
  3978. }];
  3979. // decrypt
  3980. if (company_name) {
  3981. company_name = [AESCrypt fastdecrypt:company_name];
  3982. }
  3983. if (addr_1) {
  3984. addr_1 = [AESCrypt fastdecrypt:addr_1];
  3985. }
  3986. if (phone) {
  3987. phone = [AESCrypt fastdecrypt:phone];
  3988. }
  3989. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  3990. countryCode = [iSalesDB jk_queryText:countrySql];
  3991. stateCode = state;
  3992. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  3993. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  3994. NSString *code_id = params[@"country"];
  3995. countryCode_id = code_id;
  3996. countryCode = [self countryCodeByid:code_id];
  3997. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  3998. NSString *zip_code = params[@"zipcode"];
  3999. // 剔除全部为空格
  4000. int spaceCount = 0;
  4001. for (int i = 0; i < zip_code.length; i++) {
  4002. if ([zip_code characterAtIndex:i] == ' ') {
  4003. spaceCount++;
  4004. }
  4005. }
  4006. if (spaceCount == zip_code.length) {
  4007. zip_code = @"";
  4008. }
  4009. if (zipcode.length > 0) {
  4010. zipcode = zip_code;
  4011. countryCode_id = params[@"country"];
  4012. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4013. countryCode = [dic valueForKey:@"country_code"];
  4014. stateCode = [dic valueForKey:@"state_code"];
  4015. city = [dic valueForKey:@"city"];
  4016. // zip code
  4017. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  4018. [zipDic setValue:zipcode forKey:@"value"];
  4019. [section_0 setValue:zipDic forKey:@"item_8"];
  4020. }
  4021. }
  4022. }
  4023. // 0 Country
  4024. // 1 Company Name
  4025. // 2 Contact ID
  4026. // 3 Picture
  4027. // 4 Address 1
  4028. // 5 Address 2
  4029. // 6 Address 3
  4030. // 7 Address 4
  4031. // 8 Zip Code
  4032. // 9 State/Province
  4033. // 10 City
  4034. // 11 Contact First Name
  4035. // 12 Contact Last Name
  4036. // 13 Phone
  4037. // 14 Fax
  4038. // 15 Email
  4039. // 16 Contact Notes
  4040. // 17 Price Type
  4041. // 18 Sales Rep
  4042. // country
  4043. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4044. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  4045. // company
  4046. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  4047. // contact_id
  4048. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  4049. // picture
  4050. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  4051. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  4052. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  4053. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  4054. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  4055. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  4056. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  4057. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  4058. // addr 1 2 3 4
  4059. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  4060. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  4061. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  4062. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  4063. // zip code
  4064. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  4065. // state
  4066. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4067. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  4068. // city
  4069. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  4070. // first last
  4071. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  4072. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  4073. // phone fax email
  4074. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  4075. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  4076. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  4077. // notes
  4078. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  4079. // price
  4080. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  4081. for (NSString *key in priceDic.allKeys) {
  4082. if ([key containsString:@"val_"]) {
  4083. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  4084. if ([dic[@"value"] isEqualToString:price_type]) {
  4085. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4086. [priceDic setValue:dic forKey:key];
  4087. }
  4088. }
  4089. }
  4090. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  4091. // Sales Rep
  4092. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  4093. for (NSString *key in repDic.allKeys) {
  4094. if ([key containsString:@"val_"]) {
  4095. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  4096. NSString *value = dic[@"value"];
  4097. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  4098. if (code && [code isEqualToString:sales_rep]) {
  4099. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4100. [repDic setValue:dic forKey:key];
  4101. }
  4102. }
  4103. }
  4104. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4105. [ret setValue:section_0 forKey:@"section_0"];
  4106. return [RAUtils dict2data:ret];
  4107. }
  4108. #pragma mark save contact
  4109. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  4110. {
  4111. return [self offline_saveContact:params update:YES isCustomer:YES];
  4112. }
  4113. #pragma mark category
  4114. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4115. if (ck) {
  4116. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  4117. for (NSString *key in res.allKeys) {
  4118. if (![key isEqualToString:@"count"]) {
  4119. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  4120. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4121. if ([val[@"value"] isEqualToString:ck]) {
  4122. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4123. }
  4124. [res setValue:val forKey:key];
  4125. }
  4126. }
  4127. [dic setValue:res forKey:valueKey];
  4128. }
  4129. }
  4130. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  4131. // NSString* orderCode = [params valueForKey:@"orderCode"];
  4132. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4133. NSString* category = [params valueForKey:@"category"];
  4134. if (!category || [category isEqualToString:@""]) {
  4135. category = @"%";
  4136. }
  4137. category = [self translateSingleQuote:category];
  4138. int limit = [[params valueForKey:@"limit"] intValue];
  4139. int offset = [[params valueForKey:@"offset"] intValue];
  4140. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4141. NSString *limit_str = @"";
  4142. if (limited) {
  4143. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  4144. }
  4145. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4146. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4147. sqlite3 *db = [iSalesDB get_db];
  4148. // [iSalesDB AddExFunction:db];
  4149. int count;
  4150. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  4151. 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];
  4152. double price_min = 0;
  4153. double price_max = 0;
  4154. if ([params.allKeys containsObject:@"alert"]) {
  4155. // alert
  4156. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4157. NSString *alert = params[@"alert"];
  4158. if ([alert isEqualToString:@"Display All"]) {
  4159. alert = [NSString stringWithFormat:@""];
  4160. } else {
  4161. alert = [self translateSingleQuote:alert];
  4162. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4163. }
  4164. // available
  4165. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4166. NSString *available = params[@"available"];
  4167. NSString *available_condition;
  4168. if ([available isEqualToString:@"Display All"]) {
  4169. available_condition = @"";
  4170. } else if ([available isEqualToString:@"Available Now"]) {
  4171. available_condition = @"and availability > 0";
  4172. } else {
  4173. available_condition = @"and availability == 0";
  4174. }
  4175. // best seller
  4176. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4177. NSString *best_seller = @"";
  4178. NSString *order_best_seller = @"m.name asc";
  4179. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4180. best_seller = @"and best_seller > 0";
  4181. order_best_seller = @"m.best_seller desc,m.name asc";
  4182. }
  4183. // price
  4184. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4185. NSString *price = params[@"price"];
  4186. price_min = 0;
  4187. price_max = MAXFLOAT;
  4188. if (appDelegate.user) {
  4189. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4190. NSMutableString *priceName = [NSMutableString string];
  4191. for (int i = 0; i < priceTypeArray.count; i++) {
  4192. NSString *pricetype = priceTypeArray[i];
  4193. pricetype = [self translateSingleQuote:pricetype];
  4194. if (i == 0) {
  4195. [priceName appendFormat:@"'%@'",pricetype];
  4196. } else {
  4197. [priceName appendFormat:@",'%@'",pricetype];
  4198. }
  4199. }
  4200. if ([price isEqualToString:@"Display All"]) {
  4201. price = [NSString stringWithFormat:@""];
  4202. } else if([price containsString:@"+"]){
  4203. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4204. price_min = [price doubleValue];
  4205. 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];
  4206. } else {
  4207. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4208. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4209. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4210. 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];
  4211. }
  4212. } else {
  4213. price = @"";
  4214. }
  4215. // sold_by_qty : Sold in quantities of %@
  4216. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4217. NSString *qty = params[@"sold_by_qty"];
  4218. if ([qty isEqualToString:@"Display All"]) {
  4219. qty = @"";
  4220. } else {
  4221. qty = [self translateSingleQuote:qty];
  4222. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4223. }
  4224. // cate
  4225. category = [self translateSingleQuote:category];
  4226. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  4227. // where bestseller > 0 order by bestseller desc
  4228. // sql query: alert availability(int) best_seller(int) price qty
  4229. 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];
  4230. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4231. }
  4232. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4233. if (!appDelegate.user) {
  4234. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4235. }
  4236. [ret setValue:filter forKey:@"filter"];
  4237. DebugLog(@"offline_category sql:%@",sqlQuery);
  4238. sqlite3_stmt * statement;
  4239. [ret setValue:@"2" forKey:@"result"];
  4240. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4241. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4242. // int count=0;
  4243. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4244. {
  4245. int i=0;
  4246. while (sqlite3_step(statement) == SQLITE_ROW)
  4247. {
  4248. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4249. char *name = (char*)sqlite3_column_text(statement, 0);
  4250. if(name==nil)
  4251. name="";
  4252. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4253. char *description = (char*)sqlite3_column_text(statement, 1);
  4254. if(description==nil)
  4255. description="";
  4256. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4257. int product_id = sqlite3_column_int(statement, 2);
  4258. int wid = sqlite3_column_int(statement, 3);
  4259. int closeout = sqlite3_column_int(statement, 4);
  4260. int cid = sqlite3_column_int(statement, 5);
  4261. int wisdelete = sqlite3_column_int(statement, 6);
  4262. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4263. if(wid !=0 && wisdelete != 1)
  4264. [item setValue:@"true" forKey:@"wish_exists"];
  4265. else
  4266. [item setValue:@"false" forKey:@"wish_exists"];
  4267. if(closeout==0)
  4268. [item setValue:@"false" forKey:@"is_closeout"];
  4269. else
  4270. [item setValue:@"true" forKey:@"is_closeout"];
  4271. if(cid==0)
  4272. [item setValue:@"false" forKey:@"cart_exists"];
  4273. else
  4274. [item setValue:@"true" forKey:@"cart_exists"];
  4275. [item addEntriesFromDictionary:imgjson];
  4276. // [item setValue:nsurl forKey:@"img"];
  4277. [item setValue:nsname forKey:@"name"];
  4278. [item setValue:nsdescription forKey:@"description"];
  4279. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4280. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4281. i++;
  4282. }
  4283. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4284. [ret setObject:items forKey:@"items"];
  4285. sqlite3_finalize(statement);
  4286. } else {
  4287. DebugLog(@"nothing...");
  4288. }
  4289. DebugLog(@"count:%d",count);
  4290. [iSalesDB close_db:db];
  4291. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4292. return ret;
  4293. }
  4294. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  4295. {
  4296. return [self categoryList:params limited:YES];
  4297. }
  4298. # pragma mark item search
  4299. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  4300. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4301. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  4302. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4303. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4304. // category
  4305. NSDictionary *category_menu = [self offline_category_menu];
  4306. [filter setValue:category_menu forKey:@"category"];
  4307. NSString* where= nil;
  4308. NSString* orderby= @"m.name";
  4309. if (!filterSearch) {
  4310. int covertype = [[params valueForKey:@"covertype"] intValue];
  4311. switch (covertype) {
  4312. case 0:
  4313. {
  4314. where=@"m.category like'%%#005#%%'";
  4315. break;
  4316. }
  4317. case 1:
  4318. {
  4319. where=@"m.alert like '%QS%'";
  4320. break;
  4321. }
  4322. case 2:
  4323. {
  4324. where=@"m.availability>0";
  4325. break;
  4326. }
  4327. case 3:
  4328. {
  4329. where=@"m.best_seller>0";
  4330. orderby=@"m.best_seller desc,m.name asc";
  4331. break;
  4332. }
  4333. default:
  4334. where=@"1=1";
  4335. break;
  4336. }
  4337. }
  4338. int limit = [[params valueForKey:@"limit"] intValue];
  4339. int offset = [[params valueForKey:@"offset"] intValue];
  4340. NSString *limit_str = @"";
  4341. if (limited) {
  4342. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  4343. }
  4344. sqlite3 *db = [iSalesDB get_db];
  4345. // [iSalesDB AddExFunction:db];
  4346. int count;
  4347. NSString *sqlQuery = nil;
  4348. where = [where stringByAppendingString:@" and m.is_active = 1"];
  4349. 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];
  4350. double price_min = 0;
  4351. double price_max = 0;
  4352. if (filterSearch) {
  4353. // alert
  4354. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4355. NSString *alert = params[@"alert"];
  4356. if ([alert isEqualToString:@"Display All"]) {
  4357. alert = [NSString stringWithFormat:@""];
  4358. } else {
  4359. alert = [self translateSingleQuote:alert];
  4360. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4361. }
  4362. // available
  4363. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4364. NSString *available = params[@"available"];
  4365. NSString *available_condition;
  4366. if ([available isEqualToString:@"Display All"]) {
  4367. available_condition = @"";
  4368. } else if ([available isEqualToString:@"Available Now"]) {
  4369. available_condition = @"and availability > 0";
  4370. } else {
  4371. available_condition = @"and availability == 0";
  4372. }
  4373. // best seller
  4374. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4375. NSString *best_seller = @"";
  4376. NSString *order_best_seller = @"m.name asc";
  4377. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4378. best_seller = @"and best_seller > 0";
  4379. order_best_seller = @"m.best_seller desc,m.name asc";
  4380. }
  4381. // price
  4382. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4383. NSString *price = params[@"price"];
  4384. price_min = 0;
  4385. price_max = MAXFLOAT;
  4386. if (appDelegate.user) {
  4387. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4388. NSMutableString *priceName = [NSMutableString string];
  4389. for (int i = 0; i < priceTypeArray.count; i++) {
  4390. NSString *pricetype = priceTypeArray[i];
  4391. pricetype = [self translateSingleQuote:pricetype];
  4392. if (i == 0) {
  4393. [priceName appendFormat:@"'%@'",pricetype];
  4394. } else {
  4395. [priceName appendFormat:@",'%@'",pricetype];
  4396. }
  4397. }
  4398. if ([price isEqualToString:@"Display All"]) {
  4399. price = [NSString stringWithFormat:@""];
  4400. } else if([price containsString:@"+"]){
  4401. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4402. price_min = [price doubleValue];
  4403. 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];
  4404. } else {
  4405. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4406. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4407. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4408. 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];
  4409. }
  4410. } else {
  4411. price = @"";
  4412. }
  4413. // sold_by_qty : Sold in quantities of %@
  4414. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4415. NSString *qty = params[@"sold_by_qty"];
  4416. if ([qty isEqualToString:@"Display All"]) {
  4417. qty = @"";
  4418. } else {
  4419. qty = [self translateSingleQuote:qty];
  4420. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4421. }
  4422. // category
  4423. NSString *category_id = params[@"ctgId"];
  4424. NSMutableArray *cate_id_array = nil;
  4425. NSMutableString *cateWhere = [NSMutableString string];
  4426. if ([category_id isEqualToString:@""] || !category_id) {
  4427. [cateWhere appendString:@"1 = 1"];
  4428. } else {
  4429. if ([category_id containsString:@","]) {
  4430. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  4431. } else {
  4432. cate_id_array = [@[category_id] mutableCopy];
  4433. }
  4434. /*-----------*/
  4435. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  4436. for (int i = 0; i < cate_id_array.count; i++) {
  4437. for (NSString *key0 in cateDic.allKeys) {
  4438. if ([key0 containsString:@"category_"]) {
  4439. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  4440. for (NSString *key1 in category0.allKeys) {
  4441. if ([key1 containsString:@"category_"]) {
  4442. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  4443. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4444. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  4445. cate_id_array[i] = [category1 objectForKey:@"id"];
  4446. if (i == 0) {
  4447. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  4448. } else {
  4449. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  4450. }
  4451. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4452. [category0 setValue:category1 forKey:key1];
  4453. [cateDic setValue:category0 forKey:key0];
  4454. }
  4455. }
  4456. }
  4457. }
  4458. }
  4459. }
  4460. [filter setValue:cateDic forKey:@"category"];
  4461. }
  4462. // where bestseller > 0 order by bestseller desc
  4463. // sql query: alert availability(int) best_seller(int) price qty
  4464. 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];
  4465. // count
  4466. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4467. }
  4468. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  4469. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4470. if (!appDelegate.user) {
  4471. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4472. }
  4473. [ret setValue:filter forKey:@"filter"];
  4474. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  4475. sqlite3_stmt * statement;
  4476. [ret setValue:@"2" forKey:@"result"];
  4477. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4478. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4479. // int count=0;
  4480. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4481. {
  4482. int i=0;
  4483. while (sqlite3_step(statement) == SQLITE_ROW)
  4484. {
  4485. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4486. char *name = (char*)sqlite3_column_text(statement, 0);
  4487. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4488. char *description = (char*)sqlite3_column_text(statement, 1);
  4489. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4490. int product_id = sqlite3_column_int(statement, 2);
  4491. int wid = sqlite3_column_int(statement, 3);
  4492. int closeout = sqlite3_column_int(statement, 4);
  4493. int cid = sqlite3_column_int(statement, 5);
  4494. int wisdelete = sqlite3_column_int(statement, 6);
  4495. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4496. if(wid !=0 && wisdelete != 1)
  4497. [item setValue:@"true" forKey:@"wish_exists"];
  4498. else
  4499. [item setValue:@"false" forKey:@"wish_exists"];
  4500. if(closeout==0)
  4501. [item setValue:@"false" forKey:@"is_closeout"];
  4502. else
  4503. [item setValue:@"true" forKey:@"is_closeout"];
  4504. if(cid==0)
  4505. [item setValue:@"false" forKey:@"cart_exists"];
  4506. else
  4507. [item setValue:@"true" forKey:@"cart_exists"];
  4508. [item addEntriesFromDictionary:imgjson];
  4509. // [item setValue:nsurl forKey:@"img"];
  4510. [item setValue:nsname forKey:@"fash_name"];
  4511. [item setValue:nsdescription forKey:@"description"];
  4512. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4513. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4514. i++;
  4515. }
  4516. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4517. [ret setObject:items forKey:@"items"];
  4518. sqlite3_finalize(statement);
  4519. }
  4520. [iSalesDB close_db:db];
  4521. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4522. return ret;
  4523. }
  4524. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  4525. {
  4526. return [self itemsearch:params limited:YES];
  4527. }
  4528. #pragma mark order detail
  4529. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  4530. if (str1.length == 0) {
  4531. str1 = @"&nbsp";
  4532. }
  4533. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  4534. return str;
  4535. }
  4536. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  4537. {
  4538. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  4539. [fromformatter setDateFormat:from];
  4540. NSDate *date = [fromformatter dateFromString:datetime];
  4541. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  4542. [toformatter setDateFormat:to];
  4543. NSString * ret = [toformatter stringFromDate:date];
  4544. return ret;
  4545. }
  4546. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  4547. // 把毫秒去掉
  4548. if ([dateTime containsString:@"."]) {
  4549. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  4550. }
  4551. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  4552. formatter.dateFormat = formate;
  4553. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  4554. NSDate *date = [formatter dateFromString:dateTime];
  4555. formatter.dateFormat = newFormate;
  4556. NSString *result = [formatter stringFromDate:date];
  4557. return result ? result : @"";
  4558. }
  4559. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  4560. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  4561. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  4562. }
  4563. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  4564. {
  4565. DebugLog(@"offline oderdetail params: %@",params);
  4566. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4567. int orderId = [params[@"orderId"] intValue];
  4568. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  4569. // decrypt card number and card security code
  4570. // 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 ];
  4571. 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];
  4572. sqlite3 *db = [iSalesDB get_db];
  4573. sqlite3_stmt * statement;
  4574. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  4575. NSString *nssoid = @"";
  4576. NSString* orderinfo = @"";
  4577. NSString *moreInfo = @"";
  4578. double handlingFee = 0;
  4579. double payments_and_credist = 0;
  4580. double totalPrice = 0;
  4581. double shippingFee = 0;
  4582. double lift_gate = 0;
  4583. NSString *customer_contact = @"";
  4584. NSString *customer_email = @"";
  4585. NSString *customer_fax = @"";
  4586. NSString *customer_phone = @"";
  4587. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  4588. {
  4589. if (sqlite3_step(statement) == SQLITE_ROW)
  4590. {
  4591. int order_id = sqlite3_column_int(statement, 0);
  4592. NSString *sign_url = [self textAtColumn:51 statement:statement];
  4593. ret[@"sign_url"] = sign_url;
  4594. customer_contact = [self textAtColumn:52 statement:statement];
  4595. customer_email = [self textAtColumn:53 statement:statement];
  4596. customer_phone = [self textAtColumn:54 statement:statement];
  4597. customer_fax = [self textAtColumn:55 statement:statement];
  4598. int offline_edit=sqlite3_column_int(statement, 56);
  4599. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  4600. char *soid = (char*)sqlite3_column_text(statement, 1);
  4601. if(soid==nil)
  4602. soid= "";
  4603. nssoid= [[NSString alloc]initWithUTF8String:soid];
  4604. // so#
  4605. ret[@"so#"] = nssoid;
  4606. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  4607. if(poNumber==nil)
  4608. poNumber= "";
  4609. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  4610. char *create_time = (char*)sqlite3_column_text(statement, 3);
  4611. if(create_time==nil)
  4612. create_time= "";
  4613. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  4614. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  4615. int status = sqlite3_column_int(statement, 4);
  4616. int erpStatus = sqlite3_column_int(statement, 49);
  4617. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  4618. // status
  4619. if (status > 1 && status != 3) {
  4620. status = erpStatus;
  4621. } else if (status == 3) {
  4622. status = 15;
  4623. }
  4624. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  4625. ret[@"order_status"] = nsstatus;
  4626. char *company_name = (char*)sqlite3_column_text(statement, 5);
  4627. if(company_name==nil)
  4628. company_name= "";
  4629. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  4630. // company name
  4631. ret[@"company_name"] = nscompany_name;
  4632. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  4633. if(customer_contact==nil)
  4634. customer_contact= "";
  4635. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  4636. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  4637. if(addr_1==nil)
  4638. addr_1="";
  4639. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4640. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  4641. if(addr_2==nil)
  4642. addr_2="";
  4643. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4644. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  4645. if(addr_3==nil)
  4646. addr_3="";
  4647. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4648. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  4649. if(addr_4==nil)
  4650. addr_4="";
  4651. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4652. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4653. [arr_addr addObject:nsaddr_1];
  4654. [arr_addr addObject:nsaddr_2];
  4655. [arr_addr addObject:nsaddr_3];
  4656. [arr_addr addObject:nsaddr_4];
  4657. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  4658. char *logist = (char*)sqlite3_column_text(statement, 11);
  4659. if(logist==nil)
  4660. logist= "";
  4661. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  4662. if (status == -11 || status == 10 || status == 11) {
  4663. nslogist = [self textAtColumn:59 statement:statement];
  4664. };
  4665. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  4666. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  4667. shipping = @"Shipping To Be Quoted";
  4668. } else {
  4669. shippingFee = sqlite3_column_double(statement, 12);
  4670. }
  4671. // Shipping
  4672. ret[@"Shipping"] = shipping;
  4673. int have_lift_gate = sqlite3_column_int(statement, 17);
  4674. lift_gate = sqlite3_column_double(statement, 13);
  4675. // Liftgate Fee(No loading dock)
  4676. if (!have_lift_gate) {
  4677. lift_gate = 0;
  4678. }
  4679. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  4680. if (sqlite3_column_int(statement, 57)) {
  4681. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  4682. }
  4683. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  4684. if(general_notes==nil)
  4685. general_notes= "";
  4686. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  4687. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  4688. if(internal_notes==nil)
  4689. internal_notes= "";
  4690. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  4691. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  4692. if(payment_type==nil)
  4693. payment_type= "";
  4694. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  4695. // order info
  4696. orderinfo = [self textFileName:@"order_info.html"];
  4697. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  4698. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  4699. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  4700. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  4701. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  4702. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  4703. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  4704. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  4705. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  4706. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  4707. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  4708. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  4709. NSString *payment = nil;
  4710. if([nspayment_type isEqualToString:@"Credit Card"])
  4711. {
  4712. payment = [self textFileName:@"creditcardpayment.html"];
  4713. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  4714. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  4715. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  4716. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  4717. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  4718. NSString *card_type = [self textAtColumn:42 statement:statement];
  4719. if (card_type.length > 0) { // 显示星号
  4720. card_type = @"****";
  4721. }
  4722. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  4723. if (card_number.length > 0 && card_number.length > 4) {
  4724. for (int i = 0; i < card_number.length - 4; i++) {
  4725. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  4726. }
  4727. } else {
  4728. card_number = @"";
  4729. }
  4730. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  4731. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  4732. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  4733. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  4734. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  4735. card_expiration = @"****";
  4736. }
  4737. NSString *card_city = [self textAtColumn:46 statement:statement];
  4738. NSString *card_state = [self textAtColumn:47 statement:statement];
  4739. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  4740. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  4741. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  4742. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  4743. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  4744. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  4745. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  4746. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  4747. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  4748. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  4749. }
  4750. else
  4751. {
  4752. payment=[self textFileName:@"normalpayment.html"];
  4753. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  4754. }
  4755. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  4756. ret[@"result"]= [NSNumber numberWithInt:2];
  4757. // more info
  4758. moreInfo = [self textFileName:@"more_info.html"];
  4759. /*****ship to******/
  4760. // ShipToCompany_or_&nbsp
  4761. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  4762. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  4763. NSString *shipToName = [self textAtColumn:19 statement:statement];
  4764. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  4765. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  4766. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  4767. /*****ship from******/
  4768. // ShipFromCompany_or_&nbsp
  4769. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  4770. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  4771. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  4772. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  4773. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  4774. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  4775. /*****freight to******/
  4776. // FreightBillToCompany_or_&nbsp
  4777. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  4778. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  4779. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  4780. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  4781. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  4782. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  4783. /*****merchandise to******/
  4784. // MerchandiseBillToCompany_or_&nbsp
  4785. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  4786. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  4787. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  4788. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  4789. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  4790. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  4791. /*****return to******/
  4792. // ReturnToCompany_or_&nbsp
  4793. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  4794. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  4795. NSString *returnToName = [self textAtColumn:31 statement:statement];
  4796. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  4797. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  4798. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  4799. //
  4800. // DebugLog(@"more info : %@",moreInfo);
  4801. // handling fee
  4802. handlingFee = sqlite3_column_double(statement, 33);
  4803. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  4804. if (sqlite3_column_int(statement, 58)) {
  4805. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  4806. }
  4807. //
  4808. // customer info
  4809. customerID = [self textAtColumn:36 statement:statement];
  4810. // mode
  4811. ret[@"mode"] = appDelegate.mode;
  4812. // model_count
  4813. ret[@"model_count"] = @(0);
  4814. }
  4815. sqlite3_finalize(statement);
  4816. }
  4817. [iSalesDB close_db:db];
  4818. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  4819. // "customer_email" = "Shui Hu";
  4820. // "customer_fax" = "";
  4821. // "customer_first_name" = F;
  4822. // "customer_last_name" = L;
  4823. // "customer_name" = ",da He Xiang Dong Liu A";
  4824. // "customer_phone" = "Hey Xuan Feng";
  4825. contactInfo[@"customer_phone"] = customer_phone;
  4826. contactInfo[@"customer_fax"] = customer_fax;
  4827. contactInfo[@"customer_email"] = customer_email;
  4828. NSString *first_name = @"";
  4829. NSString *last_name = @"";
  4830. if ([customer_contact isEqualToString:@""]) {
  4831. } else if ([customer_contact containsString:@" "]) {
  4832. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  4833. first_name = [customer_contact substringToIndex:first_space_index];
  4834. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  4835. }
  4836. contactInfo[@"customer_first_name"] = first_name;
  4837. contactInfo[@"customer_last_name"] = last_name;
  4838. ret[@"customerInfo"] = contactInfo;
  4839. // models
  4840. if (nssoid) {
  4841. __block double TotalCuft = 0;
  4842. __block double TotalWeight = 0;
  4843. __block int TotalCarton = 0;
  4844. __block double allItemPrice = 0;
  4845. 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];
  4846. sqlite3 *db1 = [iSalesDB get_db];
  4847. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4848. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4849. int product_id = sqlite3_column_int(stmt, 0);
  4850. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  4851. int item_id = sqlite3_column_int(stmt, 7);
  4852. NSString* Price=nil;
  4853. if(str_price==nil)
  4854. {
  4855. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  4856. if(price==nil)
  4857. Price=@"No Price.";
  4858. else
  4859. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4860. }
  4861. else
  4862. {
  4863. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4864. }
  4865. double discount = sqlite3_column_double(stmt, 2);
  4866. int item_count = sqlite3_column_int(stmt, 3);
  4867. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  4868. NSString *nsline_note=nil;
  4869. if(line_note!=nil)
  4870. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4871. // char *name = (char*)sqlite3_column_text(stmt, 5);
  4872. // NSString *nsname = nil;
  4873. // if(name!=nil)
  4874. // nsname= [[NSString alloc]initWithUTF8String:name];
  4875. NSString *nsname = [self textAtColumn:5 statement:stmt];
  4876. // char *description = (char*)sqlite3_column_text(stmt, 6);
  4877. // NSString *nsdescription=nil;
  4878. // if(description!=nil)
  4879. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4880. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  4881. // int stockUom = sqlite3_column_int(stmt, 8);
  4882. // int _id = sqlite3_column_int(stmt, 9);
  4883. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  4884. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4885. double weight=[bsubtotaljson[@"weight"] doubleValue];
  4886. int carton=[bsubtotaljson[@"carton"] intValue];
  4887. TotalCuft += cuft;
  4888. TotalWeight += weight;
  4889. TotalCarton += carton;
  4890. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  4891. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  4892. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  4893. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4894. itemjson[@"note"]=nsline_note;
  4895. itemjson[@"origin_price"] = Price;
  4896. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4897. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4898. itemjson[@"order_item_status"] = @""; // 暂时不处理
  4899. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  4900. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  4901. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  4902. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4903. if(itemjson[@"combine"] != nil)
  4904. {
  4905. // int citem=0;
  4906. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  4907. for(int bc=0;bc<bcount;bc++)
  4908. {
  4909. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4910. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4911. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4912. subTotal += uprice * modulus * item_count;
  4913. }
  4914. }
  4915. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  4916. // NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  4917. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  4918. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  4919. allItemPrice += subTotal;
  4920. }];
  4921. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  4922. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  4923. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  4924. // payments/Credits
  4925. // payments_and_credist = sqlite3_column_double(statement, 34);
  4926. payments_and_credist = allItemPrice;
  4927. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4928. // // total
  4929. // totalPrice = sqlite3_column_double(statement, 35);
  4930. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4931. } else {
  4932. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  4933. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  4934. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  4935. // payments/Credits
  4936. payments_and_credist = 0;
  4937. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4938. }
  4939. // total
  4940. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  4941. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4942. ret[@"order_info"]= orderinfo;
  4943. ret[@"more_order_info"] = moreInfo;
  4944. return [RAUtils dict2data:ret];
  4945. }
  4946. #pragma mark order list
  4947. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  4948. NSString *nsstatus = @"";
  4949. switch (status) {
  4950. case 0:
  4951. {
  4952. nsstatus=@"Temp Order";
  4953. break;
  4954. }
  4955. case 1:
  4956. {
  4957. nsstatus=@"Saved Order";
  4958. break;
  4959. }
  4960. case 2: {
  4961. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  4962. break;
  4963. }
  4964. case 3:
  4965. case 15:
  4966. {
  4967. nsstatus=@"Cancelled";
  4968. break;
  4969. }
  4970. case 10:
  4971. {
  4972. nsstatus=@"Quote Submitted";
  4973. break;
  4974. }
  4975. case 11:
  4976. {
  4977. nsstatus=@"Sales Order Submitted";
  4978. break;
  4979. }
  4980. case 12:
  4981. {
  4982. nsstatus=@"Processing";
  4983. break;
  4984. }
  4985. case 13:
  4986. {
  4987. nsstatus=@"Shipped";
  4988. break;
  4989. }
  4990. case 14:
  4991. {
  4992. nsstatus=@"Closed";
  4993. break;
  4994. }
  4995. case -11:
  4996. {
  4997. nsstatus = @"Ready For Submit";
  4998. break;
  4999. }
  5000. default:
  5001. break;
  5002. }
  5003. return nsstatus;
  5004. }
  5005. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  5006. double total = 0;
  5007. __block double payments_and_credist = 0;
  5008. __block double allItemPrice = 0;
  5009. // sqlite3 *db1 = [iSalesDB get_db];
  5010. if (so_id) {
  5011. // 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];
  5012. 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];
  5013. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5014. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5015. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5016. int product_id = sqlite3_column_int(stmt, 0);
  5017. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5018. int discount = sqlite3_column_int(stmt, 2);
  5019. int item_count = sqlite3_column_int(stmt, 3);
  5020. // int item_id = sqlite3_column_int(stmt, 7);
  5021. int item_id = sqlite3_column_int(stmt, 4);
  5022. NSString* Price=nil;
  5023. if(str_price==nil)
  5024. {
  5025. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  5026. if(price==nil)
  5027. Price=@"No Price.";
  5028. else
  5029. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5030. }
  5031. else
  5032. {
  5033. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5034. }
  5035. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  5036. itemjson[@"The unit price"]=Price;
  5037. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  5038. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  5039. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5040. if(itemjson[@"combine"] != nil)
  5041. {
  5042. // int citem=0;
  5043. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  5044. for(int bc=0;bc<bcount;bc++)
  5045. {
  5046. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5047. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5048. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5049. subTotal += uprice * modulus * item_count;
  5050. }
  5051. }
  5052. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  5053. allItemPrice += subTotal;
  5054. }];
  5055. payments_and_credist = allItemPrice;
  5056. } else {
  5057. // payments/Credits
  5058. payments_and_credist = 0;
  5059. }
  5060. // lift_gate handlingFee shippingFee
  5061. __block double lift_gate = 0;
  5062. __block double handlingFee = 0;
  5063. __block double shippingFee = 0;
  5064. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  5065. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5066. int have_lift_gate = sqlite3_column_int(stmt, 0);
  5067. if (have_lift_gate) {
  5068. lift_gate = sqlite3_column_double(stmt, 1);
  5069. }
  5070. handlingFee = sqlite3_column_double(stmt, 2);
  5071. shippingFee = sqlite3_column_double(stmt, 3);
  5072. }];
  5073. // total
  5074. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  5075. if (close) {
  5076. [iSalesDB close_db:db1];
  5077. }
  5078. return total;
  5079. }
  5080. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  5081. {
  5082. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5083. int limit = [[params valueForKey:@"limit"] intValue];
  5084. int offset = [[params valueForKey:@"offset"] intValue];
  5085. NSString* keyword = [params valueForKey:@"keyWord"];
  5086. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  5087. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  5088. NSString* where=@"1 = 1";
  5089. if(keyword.length>0)
  5090. 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]];
  5091. if (orderStatus.length > 0) {
  5092. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  5093. if (order_status_array.count == 1) {
  5094. int status_value = [[order_status_array firstObject] integerValue];
  5095. if (status_value <= 1 || status_value == 3) {
  5096. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  5097. } else {
  5098. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  5099. }
  5100. } else if (order_status_array.count > 1) {
  5101. for (int i = 0; i < order_status_array.count;i++) {
  5102. NSString *status = order_status_array[i];
  5103. NSString *condition = @" or";
  5104. if (i == 0) {
  5105. condition = @" and (";
  5106. }
  5107. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  5108. int status_value = [status integerValue];
  5109. if (status_value <= 1 || status_value == 3) {
  5110. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  5111. } else {
  5112. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  5113. }
  5114. }
  5115. where = [where stringByAppendingString:@" )"];
  5116. }
  5117. }
  5118. 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.modify_time desc,o.create_time desc limit %d offset %d" ,where, limit, offset];
  5119. // DebugLog(@"order list sql: %@",sqlQuery);
  5120. sqlite3 *db = [iSalesDB get_db];
  5121. sqlite3_stmt * statement;
  5122. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5123. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5124. {
  5125. int count=0;
  5126. while (sqlite3_step(statement) == SQLITE_ROW)
  5127. {
  5128. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  5129. int order_id = sqlite3_column_double(statement, 0);
  5130. char *soid = (char*)sqlite3_column_text(statement, 1);
  5131. if(soid==nil)
  5132. soid= "";
  5133. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  5134. int status = sqlite3_column_double(statement, 2);
  5135. int erpStatus = sqlite3_column_double(statement, 9);
  5136. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  5137. if(sales_rep==nil)
  5138. sales_rep= "";
  5139. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  5140. char *create_by = (char*)sqlite3_column_text(statement, 4);
  5141. if(create_by==nil)
  5142. create_by= "";
  5143. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  5144. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5145. if(company_name==nil)
  5146. company_name= "";
  5147. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5148. char *create_time = (char*)sqlite3_column_text(statement, 6);
  5149. if(create_time==nil)
  5150. create_time= "";
  5151. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5152. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  5153. // double total_price = sqlite3_column_double(statement, 7);
  5154. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  5155. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  5156. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5157. int offline_edit = sqlite3_column_int(statement, 10);
  5158. // ": "JH",
  5159. // "": "$8307.00",
  5160. // "": "MOB1608050001",
  5161. // "": "ArpithaT",
  5162. // "": "1st Stage Property Transformations",
  5163. // "": "JANICE SUTTON",
  5164. // "": 2255,
  5165. // "": "08/02/2016 09:49:18",
  5166. // "": 1,
  5167. // "": "Saved Order"
  5168. // "": "1470384050483",
  5169. // "model_count": "6 / 28"
  5170. item[@"sales_rep"]= nssales_rep;
  5171. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  5172. item[@"so#"]= nssoid;
  5173. item[@"create_by"]= nscreate_by;
  5174. item[@"customer_name"]= nscompany_name;
  5175. item[@"customer_contact"] = customer_contact;
  5176. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  5177. item[@"purchase_time"]= nscreate_time;
  5178. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  5179. item[@"order_status"]= nsstatus;
  5180. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  5181. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  5182. // item[@"model_count"]
  5183. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  5184. count++;
  5185. }
  5186. ret[@"count"]= [NSNumber numberWithInt:count];
  5187. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  5188. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  5189. ret[@"result"]= [NSNumber numberWithInt:2];
  5190. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  5191. ret[@"time_zone"] = @"PST";
  5192. sqlite3_finalize(statement);
  5193. }
  5194. 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];
  5195. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  5196. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5197. [iSalesDB close_db:db];
  5198. return [RAUtils dict2data:ret];
  5199. }
  5200. #pragma mark update gnotes
  5201. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  5202. {
  5203. DebugLog(@"params: %@",params);
  5204. // comments = Meyoyoyoyoyoyoy;
  5205. // orderCode = MOB1608110001;
  5206. // password = 123456;
  5207. // user = EvanK;
  5208. 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]];
  5209. int ret = [iSalesDB execSql:sql];
  5210. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5211. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5212. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5213. // [dic setValue:@"160409" forKey:@"min_ver"];
  5214. return [RAUtils dict2data:dic];
  5215. }
  5216. #pragma mark move to wishlist
  5217. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  5218. {
  5219. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5220. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5221. _id = [NSString stringWithFormat:@"(%@)",_id];
  5222. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  5223. sqlite3 *db = [iSalesDB get_db];
  5224. __block NSString *product_id = @"";
  5225. __block NSString *item_count_str = @"";
  5226. // __block NSString *item_id = nil;
  5227. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5228. // product_id = [self textAtColumn:0 statement:stmt];
  5229. int item_count = sqlite3_column_int(stmt, 1);
  5230. // item_id = [self textAtColumn:2 statement:stmt];
  5231. NSString *p_id = [self textAtColumn:0 statement:stmt];
  5232. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  5233. if (p_id.length) {
  5234. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  5235. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  5236. }
  5237. }];
  5238. [iSalesDB close_db:db];
  5239. // 去除第一个,
  5240. product_id = [product_id substringFromIndex:1];
  5241. item_count_str = [item_count_str substringFromIndex:1];
  5242. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5243. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  5244. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  5245. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  5246. sqlite3 *db1 = [iSalesDB get_db];
  5247. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5248. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5249. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  5250. // 删除
  5251. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  5252. int ret = [iSalesDB execSql:deleteSql db:db1];
  5253. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5254. [iSalesDB close_db:db1];
  5255. return [RAUtils dict2data:dic];
  5256. }
  5257. #pragma mark cart delete
  5258. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  5259. {
  5260. // cartItemId = 548;
  5261. // orderCode = MOB1608110001;
  5262. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5263. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5264. _id = [NSString stringWithFormat:@"(%@)",_id];
  5265. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  5266. int ret = [iSalesDB execSql:sql];
  5267. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5268. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5269. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5270. // [dic setValue:@"160409" forKey:@"min_ver"];
  5271. return [RAUtils dict2data:dic];
  5272. }
  5273. #pragma mark set price
  5274. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  5275. {
  5276. DebugLog(@"cart set price params: %@",params);
  5277. // "cartitem_id" = 1;
  5278. // discount = "0.000000";
  5279. // "item_note" = "";
  5280. // price = "269.000000";
  5281. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5282. NSString *notes = [self valueInParams:params key:@"item_note"];
  5283. NSString *discount = [self valueInParams:params key:@"discount"];
  5284. NSString *price = [self valueInParams:params key:@"price"];
  5285. // bool badd_price_changed=false;
  5286. // sqlite3* db=[iSalesDB get_db];
  5287. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5288. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  5289. // NSRange range;
  5290. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  5291. //
  5292. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  5293. // badd_price_changed=true;
  5294. // [iSalesDB close_db:db];
  5295. //
  5296. // if(badd_price_changed)
  5297. // {
  5298. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  5299. // }
  5300. //
  5301. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  5302. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  5303. int ret = [iSalesDB execSql:sql];
  5304. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5305. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5306. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5307. // [dic setValue:@"160409" forKey:@"min_ver"];
  5308. return [RAUtils dict2data:dic];
  5309. }
  5310. #pragma mark set line notes
  5311. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  5312. {
  5313. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5314. NSString *notes = [self valueInParams:params key:@"notes"];
  5315. notes = [self translateSingleQuote:notes];
  5316. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  5317. int ret = [iSalesDB execSql:sql];
  5318. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5319. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5320. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5321. // [dic setValue:@"160409" forKey:@"min_ver"];
  5322. return [RAUtils dict2data:dic];
  5323. }
  5324. #pragma mark set qty
  5325. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  5326. {
  5327. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5328. int item_count = [params[@"inputInt"] integerValue];
  5329. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  5330. int ret = [iSalesDB execSql:sql];
  5331. __block int item_id = 0;
  5332. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5333. item_id = sqlite3_column_int(stmt, 0);
  5334. }];
  5335. sqlite3 *db = [iSalesDB get_db];
  5336. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  5337. [iSalesDB close_db:db];
  5338. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5339. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5340. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5341. // [dic setValue:@"160409" forKey:@"min_ver"];
  5342. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  5343. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  5344. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  5345. return [RAUtils dict2data:dic];
  5346. }
  5347. #pragma mark place order
  5348. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  5349. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5350. if (aname) {
  5351. [dic setValue:aname forKey:@"aname"];
  5352. }
  5353. if (control) {
  5354. [dic setValue:control forKey:@"control"];
  5355. }
  5356. if (keyboard) {
  5357. [dic setValue:keyboard forKey:@"keyboard"];
  5358. }
  5359. if (name) {
  5360. [dic setValue:name forKey:@"name"];
  5361. }
  5362. if (value) {
  5363. [dic setValue:value forKey:@"value"];
  5364. }
  5365. return dic;
  5366. }
  5367. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  5368. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5369. orderCode = [self translateSingleQuote:orderCode];
  5370. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  5371. 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];
  5372. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5373. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  5374. NSString *img0 = [self textAtColumn:1 statement:stmt];
  5375. NSString *img1 = [self textAtColumn:2 statement:stmt];
  5376. NSString *img2 = [self textAtColumn:3 statement:stmt];
  5377. NSString *company_name = [self textAtColumn:4 statement:stmt];
  5378. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  5379. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  5380. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  5381. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  5382. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  5383. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  5384. NSString *email = [self textAtColumn:11 statement:stmt];
  5385. NSString *phone = [self textAtColumn:12 statement:stmt];
  5386. NSString *fax = [self textAtColumn:13 statement:stmt];
  5387. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  5388. NSString *city = [self textAtColumn:15 statement:stmt];
  5389. NSString *state = [self textAtColumn:16 statement:stmt];
  5390. NSString *country = [self textAtColumn:17 statement:stmt];
  5391. NSString *name = [self textAtColumn:18 statement:stmt];
  5392. // contact id
  5393. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  5394. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  5395. [contact_id_dic setValue:@"text" forKey:@"control"];
  5396. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  5397. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  5398. [contact_id_dic setValue:@"true" forKey:@"required"];
  5399. [contact_id_dic setValue:contact_id forKey:@"value"];
  5400. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  5401. // business card
  5402. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  5403. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  5404. [business_card_dic setValue:@"img" forKey:@"control"];
  5405. [business_card_dic setValue:@"1" forKey:@"disable"];
  5406. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  5407. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  5408. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  5409. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  5410. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  5411. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  5412. [business_card_dic setValue:@"business_card" forKey:@"name"];
  5413. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  5414. // fax
  5415. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5416. [customer_dic setValue:fax_dic forKey:@"item_10"];
  5417. // zipcode
  5418. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  5419. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  5420. // city
  5421. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  5422. [customer_dic setValue:city_dic forKey:@"item_12"];
  5423. // state
  5424. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  5425. [customer_dic setValue:state_dic forKey:@"item_13"];
  5426. // country
  5427. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  5428. [customer_dic setValue:country_dic forKey:@"item_14"];
  5429. // company name
  5430. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  5431. [customer_dic setValue:company_dic forKey:@"item_2"];
  5432. // addr_1
  5433. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  5434. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  5435. // addr_2
  5436. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  5437. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  5438. // addr_3
  5439. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  5440. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  5441. // addr_4
  5442. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  5443. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  5444. // Contact
  5445. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  5446. [customer_dic setValue:contact_dic forKey:@"item_7"];
  5447. // email
  5448. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5449. [customer_dic setValue:email_dic forKey:@"item_8"];
  5450. // phone
  5451. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5452. [customer_dic setValue:phone_dic forKey:@"item_9"];
  5453. // title
  5454. [customer_dic setValue:@"Customer" forKey:@"title"];
  5455. // count
  5456. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  5457. }];
  5458. // setting
  5459. NSDictionary *setting = params[@"setting"];
  5460. NSNumber *hide = setting[@"CustomerHide"];
  5461. [customer_dic setValue:hide forKey:@"hide"];
  5462. return customer_dic;
  5463. }
  5464. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5465. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5466. // setting
  5467. NSDictionary *setting = params[@"setting"];
  5468. NSNumber *hide = setting[@"ShipToHide"];
  5469. [dic setValue:hide forKey:@"hide"];
  5470. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5471. orderCode = [self translateSingleQuote:orderCode];
  5472. 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];
  5473. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5474. NSString *cid = [self textAtColumn:0 statement:stmt];
  5475. NSString *name = [self textAtColumn:1 statement:stmt];
  5476. NSString *ext = [self textAtColumn:2 statement:stmt];
  5477. NSString *contact = [self textAtColumn:3 statement:stmt];
  5478. NSString *email = [self textAtColumn:4 statement:stmt];
  5479. NSString *fax = [self textAtColumn:5 statement:stmt];
  5480. NSString *phone = [self textAtColumn:6 statement:stmt];
  5481. // count
  5482. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5483. // title
  5484. [dic setValue:@"Ship To" forKey:@"title"];
  5485. // choose
  5486. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5487. [choose_dic setValue:@"choose" forKey:@"aname"];
  5488. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5489. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5490. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5491. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5492. @"receive_contact" : @"customer_contact",
  5493. @"receive_email" : @"customer_email",
  5494. @"receive_ext" : @"customer_contact_ext",
  5495. @"receive_fax" : @"customer_fax",
  5496. @"receive_name" : @"customer_name",
  5497. @"receive_phone" : @"customer_phone"},
  5498. @"refresh" : [NSNumber numberWithInteger:1],
  5499. @"type" : @"pull"};
  5500. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  5501. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  5502. @"name" : @"Add new address",
  5503. @"refresh" : [NSNumber numberWithInteger:1],
  5504. @"value" : @"new_addr"
  5505. };
  5506. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  5507. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  5508. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5509. @"receive_contact" : @"customer_contact",
  5510. @"receive_email" : @"customer_email",
  5511. @"receive_ext" : @"customer_contact_ext",
  5512. @"receive_fax" : @"customer_fax",
  5513. @"receive_name" : @"customer_name",
  5514. @"receive_phone" : @"customer_phone"},
  5515. @"name" : @"select_ship_to",
  5516. @"refresh" : [NSNumber numberWithInteger:1],
  5517. @"value" : @"Sales_Order_Ship_To"};
  5518. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  5519. [dic setValue:choose_dic forKey:@"item_0"];
  5520. // contact id
  5521. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5522. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5523. [contact_id_dic setValue:@"true" forKey:@"required"];
  5524. [dic setValue:contact_id_dic forKey:@"item_1"];
  5525. // company name
  5526. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5527. [dic setValue:company_name_dic forKey:@"item_2"];
  5528. // address
  5529. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5530. NSNumber *required = setting[@"ShippingToAddressRequire"];
  5531. if ([required integerValue]) {
  5532. [ext_dic setValue:@"true" forKey:@"required"];
  5533. }
  5534. [dic setValue:ext_dic forKey:@"item_3"];
  5535. // contact
  5536. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5537. [dic setValue:contact_dic forKey:@"item_4"];
  5538. // phone
  5539. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5540. [dic setValue:phone_dic forKey:@"item_5"];
  5541. // fax
  5542. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5543. [dic setValue:fax_dic forKey:@"item_6"];
  5544. // email
  5545. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5546. [dic setValue:email_dic forKey:@"item_7"];
  5547. }];
  5548. return dic;
  5549. }
  5550. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  5551. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5552. // setting
  5553. NSDictionary *setting = params[@"setting"];
  5554. NSNumber *hide = setting[@"ShipFromHide"];
  5555. [dic setValue:hide forKey:@"hide"];
  5556. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5557. orderCode = [self translateSingleQuote:orderCode];
  5558. 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];
  5559. 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';";
  5560. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5561. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  5562. __block NSString *name = [self textAtColumn:1 statement:stmt];
  5563. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  5564. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  5565. __block NSString *email = [self textAtColumn:4 statement:stmt];
  5566. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  5567. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  5568. if (!cid.length) {
  5569. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  5570. cid = [self textAtColumn:0 statement:statment];
  5571. name = [self textAtColumn:1 statement:statment];
  5572. ext = [self textAtColumn:2 statement:statment];
  5573. contact = [self textAtColumn:3 statement:statment];
  5574. email = [self textAtColumn:4 statement:statment];
  5575. fax = [self textAtColumn:5 statement:statment];
  5576. phone = [self textAtColumn:6 statement:statment];
  5577. }];
  5578. }
  5579. // count
  5580. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5581. // title
  5582. [dic setValue:@"Ship From" forKey:@"title"];
  5583. // hide
  5584. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5585. // choose
  5586. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5587. [choose_dic setValue:@"choose" forKey:@"aname"];
  5588. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5589. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5590. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  5591. @"key_map" : @{@"sender_cid" : @"customer_cid",
  5592. @"sender_contact" : @"customer_contact",
  5593. @"sender_email" : @"customer_email",
  5594. @"sender_ext" : @"customer_contact_ext",
  5595. @"sender_fax" : @"customer_fax",
  5596. @"sender_name" : @"customer_name",
  5597. @"sender_phone" : @"customer_phone"},
  5598. @"name" : @"select_cid",
  5599. @"refresh" : [NSNumber numberWithInteger:0],
  5600. @"value" : @"Sales_Order_Ship_From"};
  5601. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  5602. [dic setValue:choose_dic forKey:@"item_0"];
  5603. // contact id
  5604. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5605. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5606. [contact_id_dic setValue:@"true" forKey:@"required"];
  5607. [dic setValue:contact_id_dic forKey:@"item_1"];
  5608. // company name
  5609. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5610. [dic setValue:company_name_dic forKey:@"item_2"];
  5611. // address
  5612. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5613. [dic setValue:ext_dic forKey:@"item_3"];
  5614. // contact
  5615. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5616. [dic setValue:contact_dic forKey:@"item_4"];
  5617. // phone
  5618. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5619. [dic setValue:phone_dic forKey:@"item_5"];
  5620. // fax
  5621. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5622. [dic setValue:fax_dic forKey:@"item_6"];
  5623. // email
  5624. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5625. [dic setValue:email_dic forKey:@"item_7"];
  5626. }];
  5627. return dic;
  5628. }
  5629. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  5630. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5631. // setting
  5632. NSDictionary *setting = params[@"setting"];
  5633. NSNumber *hide = setting[@"FreightBillToHide"];
  5634. [dic setValue:hide forKey:@"hide"];
  5635. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5636. orderCode = [self translateSingleQuote:orderCode];
  5637. 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];
  5638. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5639. NSString *cid = [self textAtColumn:0 statement:stmt];
  5640. NSString *name = [self textAtColumn:1 statement:stmt];
  5641. NSString *ext = [self textAtColumn:2 statement:stmt];
  5642. NSString *contact = [self textAtColumn:3 statement:stmt];
  5643. NSString *email = [self textAtColumn:4 statement:stmt];
  5644. NSString *fax = [self textAtColumn:5 statement:stmt];
  5645. NSString *phone = [self textAtColumn:6 statement:stmt];
  5646. // count
  5647. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5648. // title
  5649. [dic setValue:@"Freight Bill To" forKey:@"title"];
  5650. // hide
  5651. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5652. // choose
  5653. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5654. [choose_dic setValue:@"choose" forKey:@"aname"];
  5655. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5656. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  5657. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5658. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  5659. @"shipping_billto_contact" : @"receive_contact",
  5660. @"shipping_billto_email" : @"receive_email",
  5661. @"shipping_billto_ext" : @"receive_ext",
  5662. @"shipping_billto_fax" : @"receive_fax",
  5663. @"shipping_billto_name" : @"receive_name",
  5664. @"shipping_billto_phone" : @"receive_phone"},
  5665. @"type" : @"pull"};
  5666. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5667. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5668. @"type" : @"pull",
  5669. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  5670. @"shipping_billto_contact" : @"customer_contact",
  5671. @"shipping_billto_email" : @"customer_email",
  5672. @"shipping_billto_ext" : @"customer_contact_ext",
  5673. @"shipping_billto_fax" : @"customer_fax",
  5674. @"shipping_billto_name" : @"customer_name",
  5675. @"shipping_billto_phone" : @"customer_phone"}
  5676. };
  5677. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5678. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5679. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  5680. @"shipping_billto_contact" : @"sender_contact",
  5681. @"shipping_billto_email" : @"sender_email",
  5682. @"shipping_billto_ext" : @"sender_ext",
  5683. @"shipping_billto_fax" : @"sender_fax",
  5684. @"shipping_billto_name" : @"sender_name",
  5685. @"shipping_billto_phone" : @"sender_phone"},
  5686. @"type" : @"pull"
  5687. };
  5688. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  5689. NSDictionary *select_freight_bill_to_dic = @{
  5690. @"aname" : @"Select freight bill to",
  5691. @"name" : @"select_cid",
  5692. @"refresh" : [NSNumber numberWithInteger:0],
  5693. @"value" : @"Sales_Order_Freight_Bill_To",
  5694. @"key_map" : @{
  5695. @"shipping_billto_cid" : @"customer_cid",
  5696. @"shipping_billto_contact" : @"customer_contact",
  5697. @"shipping_billto_email" : @"customer_email",
  5698. @"shipping_billto_ext" : @"customer_contact_ext",
  5699. @"shipping_billto_fax" : @"customer_fax",
  5700. @"shipping_billto_name" : @"customer_name",
  5701. @"shipping_billto_phone" : @"customer_phone"
  5702. }
  5703. };
  5704. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  5705. [dic setValue:choose_dic forKey:@"item_0"];
  5706. // contact id
  5707. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5708. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5709. [contact_id_dic setValue:@"true" forKey:@"required"];
  5710. [dic setValue:contact_id_dic forKey:@"item_1"];
  5711. // company name
  5712. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5713. [dic setValue:company_name_dic forKey:@"item_2"];
  5714. // address
  5715. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5716. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  5717. if ([ext_required integerValue]) {
  5718. [ext_dic setValue:@"true" forKey:@"required"];
  5719. }
  5720. [dic setValue:ext_dic forKey:@"item_3"];
  5721. // contact
  5722. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5723. [dic setValue:contact_dic forKey:@"item_4"];
  5724. // phone
  5725. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5726. [dic setValue:phone_dic forKey:@"item_5"];
  5727. // fax
  5728. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5729. [dic setValue:fax_dic forKey:@"item_6"];
  5730. // email
  5731. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5732. [dic setValue:email_dic forKey:@"item_7"];
  5733. }];
  5734. return dic;
  5735. }
  5736. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5737. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5738. // setting
  5739. NSDictionary *setting = params[@"setting"];
  5740. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  5741. [dic setValue:hide forKey:@"hide"];
  5742. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5743. orderCode = [self translateSingleQuote:orderCode];
  5744. 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];
  5745. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5746. NSString *cid = [self textAtColumn:0 statement:stmt];
  5747. NSString *name = [self textAtColumn:1 statement:stmt];
  5748. NSString *ext = [self textAtColumn:2 statement:stmt];
  5749. NSString *contact = [self textAtColumn:3 statement:stmt];
  5750. NSString *email = [self textAtColumn:4 statement:stmt];
  5751. NSString *fax = [self textAtColumn:5 statement:stmt];
  5752. NSString *phone = [self textAtColumn:6 statement:stmt];
  5753. // count
  5754. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5755. // title
  5756. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  5757. // hide
  5758. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5759. // choose
  5760. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5761. [choose_dic setValue:@"choose" forKey:@"aname"];
  5762. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5763. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5764. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5765. @"key_map" : @{@"billing_cid" : @"receive_cid",
  5766. @"billing_contact" : @"receive_contact",
  5767. @"billing_email" : @"receive_email",
  5768. @"billing_ext" : @"receive_ext",
  5769. @"billing_fax" : @"receive_fax",
  5770. @"billing_name" : @"receive_name",
  5771. @"billing_phone" : @"receive_phone"},
  5772. @"type" : @"pull"};
  5773. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5774. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5775. @"type" : @"pull",
  5776. @"key_map" : @{@"billing_cid" : @"customer_cid",
  5777. @"billing_contact" : @"customer_contact",
  5778. @"billing_email" : @"customer_email",
  5779. @"billing_ext" : @"customer_contact_ext",
  5780. @"billing_fax" : @"customer_fax",
  5781. @"billing_name" : @"customer_name",
  5782. @"billing_phone" : @"customer_phone"}
  5783. };
  5784. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5785. NSDictionary *select_bill_to_dic = @{
  5786. @"aname" : @"Select bill to",
  5787. @"name" : @"select_cid",
  5788. @"refresh" : [NSNumber numberWithInteger:0],
  5789. @"value" : @"Sales_Order_Merchandise_Bill_To",
  5790. @"key_map" : @{
  5791. @"billing_cid" : @"customer_cid",
  5792. @"billing_contact" : @"customer_contact",
  5793. @"billing_email" : @"customer_email",
  5794. @"billing_ext" : @"customer_contact_ext",
  5795. @"billing_fax" : @"customer_fax",
  5796. @"billing_name" : @"customer_name",
  5797. @"billing_phone" : @"customer_phone"
  5798. }
  5799. };
  5800. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  5801. [dic setValue:choose_dic forKey:@"item_0"];
  5802. // contact id
  5803. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5804. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5805. [contact_id_dic setValue:@"true" forKey:@"required"];
  5806. [dic setValue:contact_id_dic forKey:@"item_1"];
  5807. // company name
  5808. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5809. [dic setValue:company_name_dic forKey:@"item_2"];
  5810. // address
  5811. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5812. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  5813. if ([ext_required integerValue]) {
  5814. [ext_dic setValue:@"true" forKey:@"required"];
  5815. }
  5816. [dic setValue:ext_dic forKey:@"item_3"];
  5817. // contact
  5818. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5819. [dic setValue:contact_dic forKey:@"item_4"];
  5820. // phone
  5821. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5822. [dic setValue:phone_dic forKey:@"item_5"];
  5823. // fax
  5824. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5825. [dic setValue:fax_dic forKey:@"item_6"];
  5826. // email
  5827. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5828. [dic setValue:email_dic forKey:@"item_7"];
  5829. }];
  5830. return dic;
  5831. }
  5832. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5833. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5834. // setting
  5835. NSDictionary *setting = params[@"setting"];
  5836. NSNumber *hide = setting[@"ReturnToHide"];
  5837. [dic setValue:hide forKey:@"hide"];
  5838. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5839. orderCode = [self translateSingleQuote:orderCode];
  5840. 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];
  5841. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5842. NSString *cid = [self textAtColumn:0 statement:stmt];
  5843. NSString *name = [self textAtColumn:1 statement:stmt];
  5844. NSString *ext = [self textAtColumn:2 statement:stmt];
  5845. NSString *contact = [self textAtColumn:3 statement:stmt];
  5846. NSString *email = [self textAtColumn:4 statement:stmt];
  5847. NSString *fax = [self textAtColumn:5 statement:stmt];
  5848. NSString *phone = [self textAtColumn:6 statement:stmt];
  5849. // count
  5850. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5851. // title
  5852. [dic setValue:@"Return To" forKey:@"title"];
  5853. // hide
  5854. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5855. // choose
  5856. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5857. [choose_dic setValue:@"choose" forKey:@"aname"];
  5858. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5859. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  5860. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5861. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  5862. @"returnto_contact" : @"sender_contact",
  5863. @"returnto_email" : @"sender_email",
  5864. @"returnto_ext" : @"sender_ext",
  5865. @"returnto_fax" : @"sender_fax",
  5866. @"returnto_name" : @"sender_name",
  5867. @"returnto_phone" : @"sender_phone"},
  5868. @"type" : @"pull"};
  5869. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  5870. NSDictionary *select_return_to_dic = @{
  5871. @"aname" : @"Select return to",
  5872. @"name" : @"select_cid",
  5873. @"refresh" : [NSNumber numberWithInteger:0],
  5874. @"value" : @"Contact_Return_To",
  5875. @"key_map" : @{
  5876. @"returnto_cid" : @"customer_cid",
  5877. @"returnto_contact" : @"customer_contact",
  5878. @"returnto_email" : @"customer_email",
  5879. @"returnto_ext" : @"customer_contact_ext",
  5880. @"returnto_fax" : @"customer_fax",
  5881. @"returnto_name" : @"customer_name",
  5882. @"returnto_phone" : @"customer_phone"
  5883. }
  5884. };
  5885. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  5886. [dic setValue:choose_dic forKey:@"item_0"];
  5887. // contact id
  5888. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5889. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5890. [contact_id_dic setValue:@"true" forKey:@"required"];
  5891. [dic setValue:contact_id_dic forKey:@"item_1"];
  5892. // company name
  5893. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5894. [dic setValue:company_name_dic forKey:@"item_2"];
  5895. // address
  5896. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5897. [dic setValue:ext_dic forKey:@"item_3"];
  5898. // contact
  5899. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5900. [dic setValue:contact_dic forKey:@"item_4"];
  5901. // phone
  5902. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5903. [dic setValue:phone_dic forKey:@"item_5"];
  5904. // fax
  5905. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5906. [dic setValue:fax_dic forKey:@"item_6"];
  5907. // email
  5908. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5909. [dic setValue:email_dic forKey:@"item_7"];
  5910. }];
  5911. return dic;
  5912. }
  5913. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  5914. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5915. orderCode = [self translateSingleQuote:orderCode];
  5916. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5917. __block double TotalCuft = 0;
  5918. __block double TotalWeight = 0;
  5919. __block int TotalCarton = 0;
  5920. __block double payments = 0;
  5921. // setting
  5922. NSDictionary *setting = params[@"setting"];
  5923. NSNumber *hide = setting[@"ModelInformationHide"];
  5924. [dic setValue:hide forKey:@"hide"];
  5925. 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];
  5926. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5927. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5928. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  5929. // item id
  5930. int item_id = sqlite3_column_int(stmt, 0);
  5931. // count
  5932. int item_count = sqlite3_column_int(stmt, 1);
  5933. // stockUom
  5934. int stockUom = sqlite3_column_int(stmt, 2);
  5935. // unit price
  5936. NSString *str_price = [self textAtColumn:3 statement:stmt];
  5937. NSString* Price=nil;
  5938. if([str_price isEqualToString:@""])
  5939. {
  5940. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  5941. if(price==nil)
  5942. Price=@"No Price.";
  5943. else
  5944. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5945. }
  5946. else
  5947. {
  5948. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  5949. }
  5950. // discount
  5951. double discount = sqlite3_column_double(stmt, 4);
  5952. // name
  5953. NSString *name = [self textAtColumn:5 statement:stmt];
  5954. // description
  5955. NSString *description = [self textAtColumn:6 statement:stmt];
  5956. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  5957. // line note
  5958. NSString *line_note = [self textAtColumn:7 statement:stmt];
  5959. int avaulability = sqlite3_column_int(stmt, 8);
  5960. // img
  5961. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  5962. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false];
  5963. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5964. if(combine != nil)
  5965. {
  5966. // int citem=0;
  5967. int bcount=[[combine valueForKey:@"count"] intValue];
  5968. for(int bc=0;bc<bcount;bc++)
  5969. {
  5970. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5971. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5972. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5973. subTotal += uprice * modulus * item_count;
  5974. }
  5975. }
  5976. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  5977. [model_dic setValue:@"model" forKey:@"control"];
  5978. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  5979. [model_dic setValue:description forKey:@"description"];
  5980. [model_dic setValue:line_note forKey:@"note"];
  5981. [model_dic setValue:img forKey:@"img_url"];
  5982. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  5983. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  5984. [model_dic setValue:Price forKey:@"unit_price"];
  5985. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  5986. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  5987. if (combine) {
  5988. [model_dic setValue:combine forKey:@"combine"];
  5989. }
  5990. // well,what under the row is the info for total
  5991. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  5992. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  5993. double weight=[bsubtotaljson[@"weight"] doubleValue];
  5994. int carton=[bsubtotaljson[@"carton"] intValue];
  5995. TotalCuft += cuft;
  5996. TotalWeight += weight;
  5997. TotalCarton += carton;
  5998. payments += subTotal;
  5999. //---------------------------
  6000. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  6001. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  6002. }];
  6003. [dic setValue:@"Model Information" forKey:@"title"];
  6004. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  6005. return dic;
  6006. }
  6007. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  6008. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6009. [dic setValue:@"Remarks Content" forKey:@"title"];
  6010. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6011. // setting
  6012. NSDictionary *setting = params[@"setting"];
  6013. NSNumber *hide = setting[@"RemarksContentHide"];
  6014. [dic setValue:hide forKey:@"hide"];
  6015. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6016. orderCode = [self translateSingleQuote:orderCode];
  6017. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  6018. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6019. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  6020. int mustCall = sqlite3_column_int(stmt, 1);
  6021. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  6022. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  6023. NSDictionary *po_dic = @{
  6024. @"aname" : @"PO#",
  6025. @"control" : @"edit",
  6026. @"keyboard" : @"text",
  6027. @"name" : @"poNumber",
  6028. @"value" : poNumber
  6029. };
  6030. NSDictionary *must_call_dic = @{
  6031. @"aname" : @"MUST CALL BEFORE DELIVERY",
  6032. @"control" : @"switch",
  6033. @"name" : @"must_call",
  6034. @"value" : mustCall ? @"true" : @"false"
  6035. };
  6036. NSDictionary *general_notes_dic = @{
  6037. @"aname" : @"General notes",
  6038. @"control" : @"text_view",
  6039. @"keyboard" : @"text",
  6040. @"name" : @"comments",
  6041. @"value" : generalNotes
  6042. };
  6043. NSDictionary *internal_notes_dic = @{
  6044. @"aname" : @"Internal notes",
  6045. @"control" : @"text_view",
  6046. @"keyboard" : @"text",
  6047. @"name" : @"internal_notes",
  6048. @"value" : internalNotes
  6049. };
  6050. [dic setValue:po_dic forKey:@"item_0"];
  6051. [dic setValue:must_call_dic forKey:@"item_1"];
  6052. [dic setValue:general_notes_dic forKey:@"item_2"];
  6053. [dic setValue:internal_notes_dic forKey:@"item_3"];
  6054. }];
  6055. return dic;
  6056. }
  6057. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  6058. // params
  6059. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6060. orderCode = [self translateSingleQuote:orderCode];
  6061. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6062. // setting
  6063. NSDictionary *setting = params[@"setting"];
  6064. NSNumber *hide = setting[@"OrderTotalHide"];
  6065. [dic setValue:hide forKey:@"hide"];
  6066. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  6067. __block double lift_gate_value = 0;
  6068. __block double handling_fee = 0;
  6069. __block double shipping = 0;
  6070. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6071. int lift_gate = sqlite3_column_int(stmt, 0);
  6072. lift_gate_value = sqlite3_column_double(stmt, 1);
  6073. shipping = sqlite3_column_double(stmt, 2);
  6074. handling_fee = sqlite3_column_double(stmt, 3);
  6075. if (!lift_gate) {
  6076. lift_gate_value = 0;
  6077. }
  6078. }];
  6079. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  6080. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  6081. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  6082. double payments = [[total valueForKey:@"payments"] doubleValue];
  6083. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  6084. double totalPrice = payments;
  6085. [dic setValue:@"Order Total" forKey:@"title"];
  6086. NSDictionary *payments_dic = @{
  6087. @"align" : @"right",
  6088. @"aname" : @"Payments/Credits",
  6089. @"control" : @"text",
  6090. @"name" : @"paymentsAndCredits",
  6091. @"type" : @"price",
  6092. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  6093. };
  6094. [dic setValue:payments_dic forKey:@"item_0"];
  6095. NSDictionary *handling_fee_dic = @{
  6096. @"align" : @"right",
  6097. @"aname" : @"Handling Fee",
  6098. @"control" : @"text",
  6099. @"name" : @"handling_fee_value",
  6100. @"required" : @"true",
  6101. @"type" : @"price",
  6102. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  6103. };
  6104. NSDictionary *shipping_dic = @{
  6105. @"align" : @"right",
  6106. @"aname" : @"Shipping*",
  6107. @"control" : @"text",
  6108. @"name" : @"shipping",
  6109. @"required" : @"true",
  6110. @"type" : @"price",
  6111. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  6112. };
  6113. NSDictionary *lift_gate_dic = @{
  6114. @"align" : @"right",
  6115. @"aname" : @"Liftgate Fee(No Loading Dock)",
  6116. @"control" : @"text",
  6117. @"name" : @"lift_gate_value",
  6118. @"required" : @"true",
  6119. @"type" : @"price",
  6120. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  6121. };
  6122. int item_count = 1;
  6123. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  6124. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  6125. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6126. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6127. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  6128. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6129. } else {
  6130. }
  6131. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6132. NSDictionary *total_price_dic = @{
  6133. @"align" : @"right",
  6134. @"aname" : @"Total",
  6135. @"control" : @"text",
  6136. @"name" : @"totalPrice",
  6137. @"type" : @"price",
  6138. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  6139. };
  6140. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6141. NSDictionary *total_cuft_dic = @{
  6142. @"align" : @"right",
  6143. @"aname" : @"Total Cuft",
  6144. @"control" : @"text",
  6145. @"name" : @"",
  6146. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  6147. };
  6148. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6149. NSDictionary *total_weight_dic = @{
  6150. @"align" : @"right",
  6151. @"aname" : @"Total Weight",
  6152. @"control" : @"text",
  6153. @"name" : @"",
  6154. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  6155. };
  6156. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6157. NSDictionary *total_carton_dic = @{
  6158. @"align" : @"right",
  6159. @"aname" : @"Total Carton",
  6160. @"control" : @"text",
  6161. @"name" : @"",
  6162. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  6163. };
  6164. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6165. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  6166. return dic;
  6167. }
  6168. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  6169. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6170. orderCode = [self translateSingleQuote:orderCode];
  6171. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6172. // setting
  6173. NSDictionary *setting = params[@"setting"];
  6174. NSNumber *hide = setting[@"SignatureHide"];
  6175. [dic setValue:hide forKey:@"hide"];
  6176. [dic setValue:@"Signature" forKey:@"title"];
  6177. __block NSString *pic_path = @"";
  6178. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  6179. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6180. pic_path = [self textAtColumn:0 statement:stmt];
  6181. }];
  6182. NSDictionary *pic_dic = @{
  6183. @"aname" : @"Signature",
  6184. @"avalue" :pic_path,
  6185. @"control" : @"signature",
  6186. @"name" : @"sign_picpath",
  6187. @"value" : pic_path
  6188. };
  6189. [dic setValue:pic_dic forKey:@"item_0"];
  6190. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6191. return dic;
  6192. }
  6193. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  6194. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6195. // setting
  6196. NSDictionary *setting = params[@"setting"];
  6197. NSNumber *hide = setting[@"ShippingMethodHide"];
  6198. [dic setValue:hide forKey:@"hide"];
  6199. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6200. orderCode = [self translateSingleQuote:orderCode];
  6201. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  6202. __block NSString *logist = @"";
  6203. __block NSString *lift_gate = @"";
  6204. __block int lift_gate_integer = 0;
  6205. __block NSString *logistic_note = @"";
  6206. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6207. logist = [self textAtColumn:0 statement:stmt];
  6208. lift_gate_integer = sqlite3_column_int(stmt, 1);
  6209. logistic_note = [self textAtColumn:2 statement:stmt];
  6210. }];
  6211. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6212. [dic setValue:@"Shipping Method" forKey:@"title"];
  6213. // val_0
  6214. int PERSONAL_PICK_UP_check = 0;
  6215. int USE_MY_CARRIER_check = 0;
  6216. NSString *logistic_note_text = @"";
  6217. int will_call_check = 0;
  6218. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  6219. will_call_check = 1;
  6220. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  6221. PERSONAL_PICK_UP_check = 1;
  6222. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  6223. USE_MY_CARRIER_check = 1;
  6224. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  6225. logistic_note = [logistic_note_array firstObject];
  6226. if (logistic_note_array.count > 1) {
  6227. logistic_note_text = [logistic_note_array lastObject];
  6228. }
  6229. }
  6230. }
  6231. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  6232. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  6233. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  6234. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  6235. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  6236. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6237. NSDictionary *val0_subItem_item0 = @{
  6238. @"aname" : @"Option",
  6239. @"cadedate" : @{
  6240. @"count" : [NSNumber numberWithInteger:2],
  6241. @"val_0" : @{
  6242. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  6243. @"refresh" : [NSNumber numberWithInteger:0],
  6244. @"value" : @"PERSONAL PICK UP",
  6245. @"value_id" : @"PERSONAL PICK UP"
  6246. },
  6247. @"val_1" : @{
  6248. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  6249. @"refresh" : [NSNumber numberWithInteger:0],
  6250. @"sub_item" : @{
  6251. @"count" : [NSNumber numberWithInteger:1],
  6252. @"item_0" : @{
  6253. @"aname" : @"BOL",
  6254. @"control" : @"edit",
  6255. @"keyboard" : @"text",
  6256. @"name" : @"logist_note_text",
  6257. @"refresh" : [NSNumber numberWithInteger:0],
  6258. @"required" : @"false",
  6259. @"value" : logistic_note_text
  6260. }
  6261. },
  6262. @"value" : @"USE MY CARRIER",
  6263. @"value_id" : @"USE MY CARRIER"
  6264. }
  6265. },
  6266. @"control" : @"enum",
  6267. @"name" : @"logistic_note",
  6268. @"required" : @"true",
  6269. @"single_select" : @"true"
  6270. };
  6271. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  6272. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  6273. // val_1
  6274. lift_gate = [self valueInParams:params key:@"lift_gate"];
  6275. if([lift_gate isEqualToString:@""]) {
  6276. if (lift_gate_integer == 1) {
  6277. lift_gate = @"true";
  6278. } else {
  6279. lift_gate = @"false";
  6280. }
  6281. }
  6282. int common_carrier_check = 0;
  6283. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  6284. common_carrier_check = 1;
  6285. [params setValue:lift_gate forKey:@"lift_gate"];
  6286. }
  6287. NSDictionary *val_1 = @{
  6288. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  6289. @"sub_item" : @{
  6290. @"count" : [NSNumber numberWithInteger:1],
  6291. @"item_0" : @{
  6292. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  6293. @"control" : @"switch",
  6294. @"name" : @"lift_gate",
  6295. @"refresh" : [NSNumber numberWithInteger:1],
  6296. @"required" : @"true",
  6297. @"value" : lift_gate
  6298. }
  6299. },
  6300. @"value" : @"COMMON CARRIER",
  6301. @"value_id" : @"COMMON CARRIER"
  6302. };
  6303. // cadedate
  6304. NSDictionary *cadedate = @{
  6305. @"count" : [NSNumber numberWithInteger:2],
  6306. @"val_0" : val_0,
  6307. @"val_1" : val_1
  6308. };
  6309. // item_0
  6310. NSMutableDictionary *item_0 = @{
  6311. @"aname" : @"Shipping",
  6312. @"cadedate" : cadedate,
  6313. @"control" : @"enum",
  6314. @"name" : @"logist",
  6315. @"refresh" : [NSNumber numberWithInteger:1],
  6316. @"single_select" : @"true",
  6317. }.mutableCopy;
  6318. NSNumber *required = setting[@"ShippingMethodRequire"];
  6319. if ([required integerValue]) {
  6320. [item_0 setValue:@"true" forKey:@"required"];
  6321. }
  6322. [dic setValue:item_0 forKey:@"item_0"];
  6323. return dic;
  6324. }
  6325. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  6326. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6327. orderCode = [self translateSingleQuote:orderCode];
  6328. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  6329. __block int submit_as_integer = 0;
  6330. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6331. submit_as_integer = sqlite3_column_int(stmt, 0);
  6332. }];
  6333. int check11 = 0;
  6334. int check10 = 0;
  6335. if (submit_as_integer == 11) {
  6336. check11 = 1;
  6337. }
  6338. if (submit_as_integer == 10) {
  6339. check10 = 1;
  6340. }
  6341. // section_0
  6342. NSMutableDictionary *dic = @{
  6343. @"count" : @(1),
  6344. @"item_0" : @{
  6345. @"aname" : @"Submit Order As",
  6346. @"cadedate" : @{
  6347. @"count" : @(2),
  6348. @"val_0" : @{
  6349. @"check" : [NSNumber numberWithInteger:check11],
  6350. @"value" : @"Sales Order",
  6351. @"value_id" : @(11)
  6352. },
  6353. @"val_1" : @{
  6354. @"check" : [NSNumber numberWithInteger:check10],
  6355. @"value" : @"Quote",
  6356. @"value_id" : @(10)
  6357. }
  6358. },
  6359. @"control" : @"enum",
  6360. @"name" : @"erpOrderStatus",
  6361. @"required" : @"true",
  6362. @"single_select" : @"true"
  6363. },
  6364. @"title" : @"Order Type"
  6365. }.mutableCopy;
  6366. // setting
  6367. NSDictionary *setting = params[@"setting"];
  6368. NSNumber *hide = setting[@"OrderTypeHide"];
  6369. [dic setValue:hide forKey:@"hide"];
  6370. return dic;
  6371. }
  6372. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  6373. countryCode = [self translateSingleQuote:countryCode];
  6374. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  6375. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6376. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  6377. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  6378. if (name == NULL) {
  6379. name = "";
  6380. }
  6381. if (code == NULL) {
  6382. code = "";
  6383. }
  6384. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6385. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  6386. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  6387. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6388. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  6389. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6390. }
  6391. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  6392. [container setValue:stateDic forKey:key];
  6393. }] mutableCopy];
  6394. // failure 可以不用了,一样的
  6395. if (ret.allKeys.count == 0) {
  6396. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6397. [stateDic setValue:@"Other" forKey:@"value"];
  6398. [stateDic setValue:@"" forKey:@"value_id"];
  6399. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6400. if (state_code && [@"" isEqualToString:state_code]) {
  6401. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6402. }
  6403. NSString *key = [NSString stringWithFormat:@"val_0"];
  6404. [ret setValue:stateDic forKey:key];
  6405. }
  6406. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  6407. return ret;
  6408. }
  6409. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6410. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6411. orderCode = [self translateSingleQuote:orderCode];
  6412. 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];
  6413. __block NSString *payType = @"";
  6414. __block NSString *firstName = @"";
  6415. __block NSString *lastName = @"";
  6416. __block NSString *addr1 = @"";
  6417. __block NSString *addr2 = @"";
  6418. __block NSString *zipcode = @"";
  6419. __block NSString *cardType = @"";
  6420. __block NSString *cardNumber = @"";
  6421. __block NSString *securityCode = @"";
  6422. __block NSString *month = @"";
  6423. __block NSString *year = @"";
  6424. __block NSString *city = @"";
  6425. __block NSString *state = @"";
  6426. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6427. payType = [self textAtColumn:0 statement:stmt];
  6428. firstName = [self textAtColumn:1 statement:stmt];
  6429. lastName = [self textAtColumn:2 statement:stmt];
  6430. addr1 = [self textAtColumn:3 statement:stmt];
  6431. addr2 = [self textAtColumn:4 statement:stmt];
  6432. zipcode = [self textAtColumn:5 statement:stmt];
  6433. cardType = [self textAtColumn:6 statement:stmt];
  6434. cardNumber = [self textAtColumn:7 statement:stmt];
  6435. securityCode = [self textAtColumn:8 statement:stmt];
  6436. month = [self textAtColumn:9 statement:stmt];
  6437. year = [self textAtColumn:10 statement:stmt];
  6438. city = [self textAtColumn:11 statement:stmt];
  6439. state = [self textAtColumn:12 statement:stmt];
  6440. }];
  6441. NSString *required = @"true";
  6442. // setting
  6443. NSDictionary *setting = params[@"setting"];
  6444. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  6445. if ([requiredNumber integerValue]) {
  6446. required = @"true";
  6447. } else {
  6448. required = @"false";
  6449. }
  6450. // "section_2"
  6451. NSMutableDictionary *dic = @{
  6452. @"count" : @(1),
  6453. @"item_0" : @{
  6454. @"aname" : @"Payment",
  6455. @"required" : required,
  6456. @"cadedate" : @{
  6457. @"count" : @(6),
  6458. @"val_0" : @{
  6459. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  6460. @"value" : @"Check",
  6461. @"value_id" : @"Check"
  6462. },
  6463. @"val_1" : @{
  6464. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  6465. @"value" : @"Cash",
  6466. @"value_id" : @"Cash"
  6467. },
  6468. @"val_2" : @{
  6469. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  6470. @"value" : @"NET 30",
  6471. @"value_id" : @"NET 30"
  6472. },
  6473. @"val_3" : @{
  6474. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  6475. @"value" : @"Wire Transfer",
  6476. @"value_id" : @"Wire Transfer"
  6477. },
  6478. @"val_4" : @{
  6479. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  6480. @"sub_item" : @{
  6481. @"count" : @(3),
  6482. @"item_0" : @{
  6483. @"aname" : @"choose",
  6484. @"control" : @"multi_action",
  6485. @"count" : @(1),
  6486. @"item_0" : @{
  6487. @"aname" : @"Same as customer",
  6488. @"key_map" : @{
  6489. @"credit_card_address1" : @"customer_address1",
  6490. @"credit_card_address2" : @"customer_address2",
  6491. @"credit_card_city" : @"customer_city",
  6492. @"credit_card_first_name" : @"customer_first_name",
  6493. @"credit_card_last_name" : @"customer_last_name",
  6494. @"credit_card_state" : @"customer_state",
  6495. @"credit_card_zipcode" : @"customer_zipcode"
  6496. },
  6497. @"type" : @"pull"
  6498. }
  6499. },
  6500. @"item_1" : @{
  6501. @"aname" : @"",
  6502. @"color" : @"red",
  6503. @"control" : @"text",
  6504. @"name" : @"",
  6505. @"value" : @"USA Credit cards only"
  6506. },
  6507. @"item_2" : @{
  6508. @"aname" : @"Fill",
  6509. @"cadedate" : @{
  6510. @"count" : @(2),
  6511. @"val_0" : @{
  6512. @"check" : @(1),
  6513. @"sub_item" : @{
  6514. @"count" : @(11),
  6515. @"item_0" : @{
  6516. @"aname" : @"Type",
  6517. @"cadedate" : @{
  6518. @"count" : @(2),
  6519. @"val_0" : @{
  6520. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  6521. @"value" : @"VISA",
  6522. @"value_id" : @(0)
  6523. },
  6524. @"val_1" : @{
  6525. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  6526. @"value" : @"MASTER CARD",
  6527. @"value_id" : @(1)
  6528. }
  6529. },
  6530. @"control" : @"enum",
  6531. @"name" : @"credit_card_type",
  6532. @"required" : @"true",
  6533. @"single_select" : @"true"
  6534. },
  6535. @"item_1" : @{
  6536. @"aname" : @"Number",
  6537. @"control" : @"edit",
  6538. @"keyboard" : @"int",
  6539. @"length" : @"16",
  6540. @"name" : @"credit_card_number",
  6541. @"required" : @"true",
  6542. @"value" : cardNumber
  6543. },
  6544. @"item_10" : @{
  6545. @"aname" : @"State",
  6546. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  6547. @"control" : @"enum",
  6548. @"enum" : @"true",
  6549. @"name" : @"credit_card_state",
  6550. @"required" : @"true",
  6551. @"single_select" : @"true"
  6552. },
  6553. @"item_2" : @{
  6554. @"aname" : @"Expiration Date",
  6555. @"control" : @"monthpicker",
  6556. @"name" : @"credit_card_expiration",
  6557. @"required" : @"true",
  6558. @"type" : @"date",
  6559. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  6560. },
  6561. @"item_3" : @{
  6562. @"aname" : @"Security Code",
  6563. @"control" : @"edit",
  6564. @"keyboard" : @"int",
  6565. @"length" : @"3",
  6566. @"name" : @"credit_card_security_code",
  6567. @"required" : @"true",
  6568. @"value" : securityCode
  6569. },
  6570. @"item_4" : @{
  6571. @"aname" : @"First Name",
  6572. @"control" : @"edit",
  6573. @"keyboard" : @"text",
  6574. @"name" : @"credit_card_first_name",
  6575. @"required" : @"true",
  6576. @"value" : firstName
  6577. },
  6578. @"item_5" : @{
  6579. @"aname" : @"Last Name",
  6580. @"control" : @"edit",
  6581. @"keyboard" : @"text",
  6582. @"name" : @"credit_card_last_name",
  6583. @"required" : @"true",
  6584. @"value" : lastName
  6585. },
  6586. @"item_6" : @{
  6587. @"aname" : @"Address 1",
  6588. @"control" : @"edit",
  6589. @"keyboard" : @"text",
  6590. @"name" : @"credit_card_address1",
  6591. @"required" : @"true",
  6592. @"value" : addr1
  6593. },
  6594. @"item_7" : @{
  6595. @"aname" : @"Address 2",
  6596. @"control" : @"edit",
  6597. @"keyboard" : @"text",
  6598. @"name" : @"credit_card_address2",
  6599. @"value" : addr2
  6600. },
  6601. @"item_8" : @{
  6602. @"aname" : @"zip code",
  6603. @"control" : @"edit",
  6604. @"keyboard" : @"text",
  6605. @"name" : @"credit_card_zipcode",
  6606. @"required" : @"true",
  6607. @"value" : zipcode
  6608. },
  6609. @"item_9" : @{
  6610. @"aname" : @"City",
  6611. @"control" : @"edit",
  6612. @"keyboard" : @"text",
  6613. @"name" : @"credit_card_city",
  6614. @"required" : @"true",
  6615. @"value" : city
  6616. }
  6617. },
  6618. @"value" : @"Fill Now",
  6619. @"value_id" : @""
  6620. },
  6621. @"val_1" : @{
  6622. @"check" : @(0),
  6623. @"value" : @"Fill Later",
  6624. @"value_id" : @""
  6625. }
  6626. },
  6627. @"control" : @"enum",
  6628. @"name" : @"",
  6629. @"single_select" : @"true"
  6630. }
  6631. },
  6632. @"value" : @"Credit Card",
  6633. @"value_id" : @"Credit Card"
  6634. },
  6635. @"val_5" : @{
  6636. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  6637. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  6638. @"value_id" : @"FOLLOW EXISTING"
  6639. }
  6640. },
  6641. @"control" : @"enum",
  6642. @"name" : @"paymentType",
  6643. @"single_select" : @"true"
  6644. },
  6645. @"title" : @"Payment Information"
  6646. }.mutableCopy;
  6647. NSNumber *hide = setting[@"PaymentInformationHide"];
  6648. [dic setValue:hide forKey:@"hide"];
  6649. return dic;
  6650. }
  6651. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  6652. {
  6653. sqlite3 *db = [iSalesDB get_db];
  6654. // UISetting
  6655. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  6656. NSString *cachefolder = [paths objectAtIndex:0];
  6657. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  6658. NSData* json =nil;
  6659. json=[NSData dataWithContentsOfFile:img_cache];
  6660. NSError *error=nil;
  6661. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  6662. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  6663. [params setObject:setting forKey:@"setting"];
  6664. int section_count = 0;
  6665. // params
  6666. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6667. orderCode = [self translateSingleQuote:orderCode];
  6668. // 0 Order Type 1 Shipping Method 2 Payment Information
  6669. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  6670. // 0 Order Type
  6671. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  6672. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  6673. [ret setValue:order_type_dic forKey:key0];
  6674. // 1 Shipping Method
  6675. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  6676. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  6677. [ret setValue:shipping_method_dic forKey:key1];
  6678. // 2 Payment Information
  6679. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  6680. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  6681. [ret setValue:payment_info_dic forKey:key2];
  6682. // 3 Customer
  6683. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  6684. NSDictionary *customer_dic = [self customerDic:params db:db];
  6685. [ret setValue:customer_dic forKey:key3];
  6686. // 4 Ship To
  6687. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  6688. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  6689. [ret setValue:ship_to_dic forKey:key4];
  6690. // 5 Ship From
  6691. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  6692. if (![shipFromDisable integerValue]) {
  6693. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  6694. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  6695. [ret setValue:ship_from_dic forKey:key5];
  6696. }
  6697. // 6 Freight Bill To
  6698. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  6699. if (![freightBillToDisable integerValue]) {
  6700. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  6701. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  6702. [ret setValue:freight_bill_to forKey:key6];
  6703. }
  6704. // 7 Merchandise Bill To
  6705. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  6706. if (![merchandiseBillToDisable integerValue]) {
  6707. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  6708. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  6709. [ret setValue:merchandise_bill_to_dic forKey:key7];
  6710. }
  6711. // 8 Return To
  6712. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  6713. if (![returnToDisable integerValue]) {
  6714. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  6715. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  6716. [ret setValue:return_to_dic forKey:key8];
  6717. }
  6718. // 9 Model Information
  6719. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  6720. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  6721. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  6722. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  6723. [ret setValue:model_info_dic forKey:key9];
  6724. // 10 Remarks Content
  6725. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  6726. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  6727. [ret setValue:remarks_content_dic forKey:key10];
  6728. // 11 Order Total
  6729. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  6730. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  6731. [ret setValue:order_total_dic forKey:key11];
  6732. // 12 Signature
  6733. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  6734. NSDictionary *sign_dic = [self signatureDic:params db:db];
  6735. [ret setValue:sign_dic forKey:key12];
  6736. [ret setValue:@(section_count) forKey:@"section_count"];
  6737. [iSalesDB close_db:db];
  6738. return [RAUtils dict2data:ret];
  6739. // return nil;
  6740. }
  6741. #pragma mark addr editor
  6742. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  6743. NSMutableDictionary *new_item = [item mutableCopy];
  6744. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  6745. return new_item;
  6746. }
  6747. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  6748. {
  6749. // "is_subaction" = true;
  6750. // orderCode = MOB1608240002;
  6751. // password = 123456;
  6752. // "subaction_tag" = 1;
  6753. // user = EvanK;
  6754. // {
  6755. // "is_subaction" = true;
  6756. // orderCode = MOB1608240002;
  6757. // password = 123456;
  6758. // "refresh_trigger" = zipcode;
  6759. // "subaction_tag" = 1;
  6760. // user = EvanK;
  6761. // }
  6762. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  6763. [ret removeObjectForKey:@"up_params"];
  6764. [ret setObject:@"New Address" forKey:@"title"];
  6765. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  6766. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  6767. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  6768. NSMutableDictionary *country_dic = [section_0[@"item_5"] mutableCopy];
  6769. [country_dic removeObjectForKey:@"refresh"];
  6770. [country_dic removeObjectForKey:@"restore"];
  6771. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  6772. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  6773. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_6"] add:YES] forKey:@"item_2"]; // Address 1*
  6774. [new_section_0 setObject:section_0[@"item_7"] forKey:@"item_3"]; // Address 2
  6775. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_4"]; // Address 3
  6776. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_5"]; // Address 4
  6777. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_10"] add:YES];
  6778. [zip_code_dic removeObjectForKey:@"refresh"];
  6779. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  6780. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_11"] add:YES] forKey:@"item_7"]; // State/Province*
  6781. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_8"]; // City*
  6782. [new_section_0 setObject:section_0[@"item_13"] forKey:@"item_9"]; // Contact First Name
  6783. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_10"]; // Contact Last Name
  6784. [new_section_0 setObject:section_0[@"item_4"] forKey:@"item_11"]; // Phone*
  6785. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_12"]; // Fax
  6786. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_3"] add:NO] forKey:@"item_13"]; // Email
  6787. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  6788. // country
  6789. NSString *countryCode = @"US";
  6790. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6791. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  6792. // state
  6793. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:nil] mutableCopy];
  6794. NSDictionary *tmpDic = @{
  6795. @"value" : @"Other",
  6796. @"value_id" : @"",
  6797. @"check" : [NSNumber numberWithInteger:0]
  6798. };
  6799. for (int i = 0; i < allState.allKeys.count; i++) {
  6800. NSString *key = [NSString stringWithFormat:@"val_%d",i];
  6801. NSDictionary *tmp = allState[key];
  6802. [allState setValue:tmpDic forKey:key];
  6803. tmpDic = tmp;
  6804. }
  6805. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  6806. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  6807. [ret setValue:new_section_0 forKey:@"section_0"];
  6808. return [RAUtils dict2data:ret];
  6809. }
  6810. #pragma mark save addr
  6811. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  6812. {
  6813. // NSString *companyName = [self valueInParams:params key:@"company"];
  6814. // NSString *addr1 = [self valueInParams:params key:@"address"];
  6815. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  6816. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  6817. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  6818. // NSString *countryId = [self valueInParams:params key:@"country"];
  6819. // NSString *stateCode = [self valueInParams:params key:@"state"];
  6820. // NSString *city = [self valueInParams:params key:@"city"];
  6821. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  6822. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  6823. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  6824. // NSString *phone = [self valueInParams:params key:@"phone"];
  6825. // NSString *fax = [self valueInParams:params key:@"fax"];
  6826. // NSString *email = [self valueInParams:params key:@"email"];
  6827. return [self offline_saveContact:params update:NO isCustomer:NO];
  6828. }
  6829. #pragma mark cancel order
  6830. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  6831. {
  6832. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6833. NSString *order_id = [self valueInParams:params key:@"orderId"];
  6834. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  6835. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  6836. int ret = [iSalesDB execSql:sql];
  6837. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6838. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6839. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6840. // [dic setValue:@"160409" forKey:@"min_ver"];
  6841. return [RAUtils dict2data:dic];
  6842. }
  6843. #pragma mark sign order
  6844. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  6845. {
  6846. //参考 offline_saveBusinesscard
  6847. NSLog(@"sign order params: %@",params);
  6848. // orderCode = MOB1608240002;
  6849. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  6850. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6851. orderCode = [self translateSingleQuote:orderCode];
  6852. NSString *picPath = [self valueInParams:params key:@"picpath"];
  6853. picPath = [self translateSingleQuote:picPath];
  6854. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  6855. int ret = [iSalesDB execSql:sql];
  6856. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6857. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6858. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6859. // [dic setValue:@"160409" forKey:@"min_ver"];
  6860. return [RAUtils dict2data:dic];
  6861. }
  6862. #pragma mark save order
  6863. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  6864. NSString *ret = [self valueInParams:params key:key];
  6865. if (translate) {
  6866. ret = [self translateSingleQuote:ret];
  6867. }
  6868. return ret;
  6869. }
  6870. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  6871. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  6872. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  6873. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  6874. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  6875. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  6876. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  6877. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  6878. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  6879. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  6880. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  6881. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  6882. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  6883. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  6884. if (![total_price isEqualToString:@""]) {
  6885. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  6886. } else {
  6887. total_price = @"";
  6888. }
  6889. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  6890. if ([paymentsAndCredits isEqualToString:@""]) {
  6891. paymentsAndCredits = @"";
  6892. } else {
  6893. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  6894. }
  6895. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  6896. if ([handling_fee_value isEqualToString:@""]) {
  6897. handling_fee_value = @"";
  6898. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  6899. handling_fee_value = @"";
  6900. } else {
  6901. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  6902. }
  6903. NSString *handling_fee_placeholder = handling_fee_value;
  6904. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  6905. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  6906. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  6907. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  6908. NSString *lift_gate_placeholder = @"";
  6909. if ([lift_gate_value isEqualToString:@""]) {
  6910. lift_gate_placeholder = @"";
  6911. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  6912. lift_gate_placeholder = @"";
  6913. } else {
  6914. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  6915. }
  6916. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  6917. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  6918. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  6919. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  6920. 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];
  6921. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  6922. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  6923. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  6924. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  6925. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  6926. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  6927. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  6928. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  6929. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  6930. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  6931. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  6932. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  6933. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  6934. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  6935. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  6936. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  6937. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  6938. BOOL number_nil = [credit_card_number isEqualToString:@""];
  6939. if (!number_nil) {
  6940. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  6941. }
  6942. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  6943. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  6944. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  6945. if (!security_code_nil) {
  6946. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  6947. }
  6948. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  6949. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  6950. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  6951. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  6952. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  6953. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  6954. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  6955. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  6956. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  6957. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  6958. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  6959. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  6960. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  6961. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  6962. NSString *contact_id = customer_cid;
  6963. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  6964. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  6965. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  6966. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  6967. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  6968. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  6969. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  6970. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  6971. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  6972. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  6973. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  6974. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  6975. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  6976. //
  6977. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  6978. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  6979. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  6980. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  6981. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  6982. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  6983. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  6984. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  6985. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  6986. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  6987. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  6988. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  6989. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  6990. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  6991. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  6992. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  6993. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  6994. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  6995. 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];
  6996. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  6997. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  6998. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  6999. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  7000. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  7001. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  7002. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  7003. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  7004. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  7005. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  7006. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  7007. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  7008. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  7009. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  7010. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  7011. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  7012. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  7013. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  7014. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  7015. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  7016. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  7017. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  7018. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  7019. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  7020. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  7021. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  7022. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  7023. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  7024. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  7025. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  7026. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  7027. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  7028. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  7029. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  7030. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  7031. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  7032. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  7033. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  7034. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  7035. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  7036. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  7037. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  7038. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  7039. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  7040. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  7041. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  7042. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  7043. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  7044. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  7045. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  7046. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  7047. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  7048. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  7049. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  7050. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  7051. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  7052. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  7053. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  7054. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  7055. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  7056. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  7057. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  7058. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  7059. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  7060. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  7061. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  7062. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  7063. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  7064. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  7065. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  7066. NSString *order_status = @"status = 1,";
  7067. if (submit) {
  7068. order_status = @"status = -11,";
  7069. }
  7070. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  7071. NSString *sync_sql = @"";
  7072. if (submit) {
  7073. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  7074. [param setValue:sales_rep forKey:@"sales_rep"];
  7075. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  7076. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  7077. }
  7078. 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];
  7079. DebugLog(@"save order contactSql: %@",contactSql);
  7080. DebugLog(@"save order orderSql: %@",orderSql);
  7081. // int contact_ret = [iSalesDB execSql:contactSql];
  7082. int order_ret = [iSalesDB execSql:orderSql];
  7083. int ret = order_ret;
  7084. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7085. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7086. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7087. // [dic setValue:@"160409" forKey:@"min_ver"];
  7088. [dic setObject:so_id forKey:@"so#"];
  7089. return [RAUtils dict2data:dic];
  7090. }
  7091. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  7092. {
  7093. // id foo = nil;
  7094. // NSMutableArray *a = @[].mutableCopy;
  7095. // [a addObject:foo];
  7096. return [self saveorder:param submit:NO];
  7097. }
  7098. #pragma mark add to cart by name
  7099. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  7100. {
  7101. NSString *orderCode = [params objectForKey:@"orderCode"];
  7102. NSString *product_name = [params objectForKey:@"product_name"];
  7103. product_name = [self translateSingleQuote:product_name];
  7104. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  7105. sqlite3 *db = [iSalesDB get_db];
  7106. __block NSMutableString *product_id_string = [NSMutableString string];
  7107. for (int i = 0; i < product_name_array.count; i++) {
  7108. NSString *name = [product_name_array objectAtIndex:i];
  7109. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  7110. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7111. int product_id = sqlite3_column_int(stmt, 0);
  7112. if (i == product_name_array.count - 1) {
  7113. [product_id_string appendFormat:@"%d",product_id];
  7114. } else {
  7115. [product_id_string appendFormat:@"%d,",product_id];
  7116. }
  7117. }];
  7118. }
  7119. NSDictionary *newParams = @{
  7120. @"product_id" : product_id_string,
  7121. @"orderCode" : orderCode
  7122. };
  7123. [iSalesDB close_db:db];
  7124. return [self offline_add2cart:[newParams mutableCopy]];
  7125. }
  7126. #pragma mark reset order
  7127. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  7128. {
  7129. UIApplication * app = [UIApplication sharedApplication];
  7130. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  7131. appDelegate.disable_trigger=true;
  7132. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  7133. appDelegate.disable_trigger=false;
  7134. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7135. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  7136. return [RAUtils dict2data:dic];
  7137. }
  7138. #pragma mark submit order
  7139. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  7140. {
  7141. return [self saveorder:params submit:YES];
  7142. }
  7143. #pragma mark copy order
  7144. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  7145. {
  7146. NSMutableDictionary *ret = @{}.mutableCopy;
  7147. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  7148. sqlite3 *db = [iSalesDB get_db];
  7149. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7150. // 首先查看联系人是否active
  7151. 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];
  7152. __block int customer_is_active = 1;
  7153. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7154. customer_is_active = sqlite3_column_int(stmt, 0);
  7155. }];
  7156. if (!customer_is_active) {
  7157. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7158. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7159. [iSalesDB close_db:db];
  7160. return [RAUtils dict2data:ret];
  7161. }
  7162. // new order
  7163. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  7164. NSString *new_order_code = [self get_offline_soid:db];
  7165. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  7166. user = [self translateSingleQuote:user];
  7167. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,total_price,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) select '%@',1,submit_as,internal_notes,sales_rep,'%@',total_price,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 from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  7168. __block int result = 1;
  7169. result = [iSalesDB execSql:insert_order_sql db:db];
  7170. if (!result) {
  7171. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7172. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7173. [iSalesDB close_db:db];
  7174. return [RAUtils dict2data:ret];
  7175. }
  7176. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  7177. // __block NSString *product_id = @"";
  7178. __weak typeof(self) weakSelf = self;
  7179. 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];
  7180. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7181. int is_active = sqlite3_column_int(stmt, 1);
  7182. if (is_active) {
  7183. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  7184. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  7185. 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];
  7186. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  7187. }
  7188. }];
  7189. // product_id = [product_id substringFromIndex:1];
  7190. //
  7191. // [self offline_add2cart:@{}.mutableCopy];
  7192. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7193. [iSalesDB close_db:db];
  7194. if (result) {
  7195. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7196. } else {
  7197. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7198. }
  7199. [ret setObject:new_order_code forKey:@"so_id"];
  7200. return [RAUtils dict2data:ret];
  7201. }
  7202. #pragma mark move wish list to cart
  7203. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  7204. NSString *collectId = params[@"collectId"];
  7205. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  7206. __block NSString *product_id = @"";
  7207. __block NSString *qty = @"";
  7208. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7209. int productId = sqlite3_column_int(stmt, 0);
  7210. int item_qty = sqlite3_column_int(stmt, 1);
  7211. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  7212. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  7213. }];
  7214. product_id = [product_id substringFromIndex:1];
  7215. qty = [qty substringFromIndex:1];
  7216. NSDictionary *newParams = @{
  7217. @"product_id" : product_id,
  7218. @"orderCode" : params[@"orderCode"],
  7219. @"qty" : qty
  7220. };
  7221. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  7222. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  7223. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  7224. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  7225. //
  7226. // int ret = [iSalesDB execSql:deleteSQL];
  7227. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  7228. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  7229. }
  7230. return [RAUtils dict2data:retDic];
  7231. }
  7232. #pragma mark - portfolio
  7233. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  7234. int sort = [[params valueForKey:@"sort"] integerValue];
  7235. int offset = [[params valueForKey:@"offset"] integerValue];
  7236. int limit = [[params valueForKey:@"limit"] integerValue];
  7237. NSString *orderBy = @"";
  7238. switch (sort) {
  7239. case 0:{
  7240. orderBy = @"modify_time desc";
  7241. }
  7242. break;
  7243. case 1:{
  7244. orderBy = @"modify_time asc";
  7245. }
  7246. break;
  7247. case 2:{
  7248. orderBy = @"name asc";
  7249. }
  7250. break;
  7251. case 3:{
  7252. orderBy = @"name desc";
  7253. }
  7254. break;
  7255. case 4:{
  7256. orderBy = @"description asc";
  7257. }
  7258. break;
  7259. default:
  7260. break;
  7261. }
  7262. 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];
  7263. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7264. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7265. sqlite3 *db = [iSalesDB get_db];
  7266. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7267. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7268. int product_id = sqlite3_column_int(stmt, 0);
  7269. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7270. NSString *name = [self textAtColumn:1 statement:stmt];
  7271. NSString *description = [self textAtColumn:2 statement:stmt];
  7272. double price = sqlite3_column_double(stmt, 3);
  7273. double discount = sqlite3_column_double(stmt,4);
  7274. int qty = sqlite3_column_int(stmt, 5);
  7275. int percentage = sqlite3_column_int(stmt, 6);
  7276. int item_id = sqlite3_column_int(stmt, 7);
  7277. // int fashion_id = sqlite3_column_int(stmt, 8);
  7278. NSString *img_path = [self textAtColumn:9 statement:stmt];
  7279. NSString *line_note = [self textAtColumn:10 statement:stmt];
  7280. double percent = sqlite3_column_double(stmt, 11);
  7281. NSString *price_null = [self textAtColumn:3 statement:stmt];
  7282. if ([price_null isEqualToString:@"null"]) {
  7283. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7284. }
  7285. NSMutableDictionary *item = @{
  7286. @"linenotes": line_note,
  7287. @"check": @(1),
  7288. @"product_id": product_id_string,
  7289. @"available_qty": @(qty),
  7290. @"available_percent" : @(percent),
  7291. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  7292. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7293. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  7294. @"img": img_path,
  7295. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  7296. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  7297. }.mutableCopy;
  7298. if (percentage) {
  7299. [item removeObjectForKey:@"available_qty"];
  7300. } else {
  7301. [item removeObjectForKey:@"available_percent"];
  7302. }
  7303. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  7304. if ([qty_null isEqualToString:@"null"]) {
  7305. [item removeObjectForKey:@"available_qty"];
  7306. }
  7307. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7308. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7309. }];
  7310. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  7311. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7312. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  7313. }
  7314. [iSalesDB close_db:db];
  7315. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  7316. [dic setValue:@"" forKey:@"email_content"];
  7317. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  7318. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7319. return [RAUtils dict2data:dic];
  7320. }
  7321. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  7322. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  7323. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  7324. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  7325. tear_name = [self translateSingleQuote:tear_name];
  7326. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  7327. tear_note = [self translateSingleQuote:tear_note];
  7328. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  7329. NSString *pdf_path = @"";
  7330. if (direct) {
  7331. configureParams = [self valueInParams:params key:@"pdfUrl"];
  7332. } else {
  7333. pdf_path = [self valueInParams:params key:@"pdfPath"];
  7334. }
  7335. pdf_path = [self translateSingleQuote:pdf_path];
  7336. NSString *create_user = [self valueInParams:params key:@"user"];
  7337. create_user = [self translateSingleQuote:create_user];
  7338. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  7339. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  7340. // model info
  7341. NSString *sql = [NSString stringWithFormat:@"select product_id,ifnull(sheet_price,'null'),sheet_discount,ifnull(available_qty,'null'),percentage,item_id,line_note,percent from offline_portfolio where product_id in (%@);",product_ids];
  7342. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7343. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7344. sqlite3 *db = [iSalesDB get_db];
  7345. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7346. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7347. int product_id = sqlite3_column_int(stmt, 0);
  7348. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7349. double price = sqlite3_column_double(stmt, 1);
  7350. double discount = sqlite3_column_double(stmt,2);
  7351. int qty = sqlite3_column_int(stmt, 3);
  7352. int percentage = sqlite3_column_int(stmt, 4);
  7353. int item_id = sqlite3_column_int(stmt, 5);
  7354. NSString *line_note = [self textAtColumn:6 statement:stmt];
  7355. double percent = sqlite3_column_double(stmt, 7);
  7356. NSString *price_null = [self textAtColumn:3 statement:stmt];
  7357. if ([price_null isEqualToString:@"null"]) {
  7358. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7359. }
  7360. NSMutableDictionary *item = @{
  7361. @"linenotes": line_note,
  7362. @"product_id": product_id_string,
  7363. @"available_qty": @(qty),
  7364. @"available_percent" : @(percent),
  7365. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7366. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  7367. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  7368. }.mutableCopy;
  7369. if (percentage) {
  7370. [item removeObjectForKey:@"available_qty"];
  7371. } else {
  7372. [item removeObjectForKey:@"available_percent"];
  7373. }
  7374. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  7375. if ([qty_null isEqualToString:@"null"]) {
  7376. [item removeObjectForKey:@"available_qty"];
  7377. }
  7378. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7379. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7380. }];
  7381. [iSalesDB close_db:db];
  7382. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  7383. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  7384. return [RAUtils dict2data:resultDictionary];
  7385. }
  7386. NSString *model_info = [RAUtils dict2string:dic];
  7387. model_info = [self translateSingleQuote:model_info];
  7388. // 创建PDF
  7389. // 在preview情况下保存,则不需要新建了
  7390. if (direct) {
  7391. NSData *pdfData = [self offline_request_tearsheet:params]; // 新建PDF,获取本地路径
  7392. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  7393. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  7394. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  7395. resultDictionary = pdfInfo.mutableCopy;
  7396. } else { // 创建PDF失败
  7397. return pdfData;
  7398. }
  7399. } else {
  7400. // pdf_path 就是本地路径
  7401. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  7402. }
  7403. // 将文件移动到PDF Cache文件夹
  7404. NSString *newPath = [pdf_path lastPathComponent];
  7405. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7406. NSString *cachefolder = [paths objectAtIndex:0];
  7407. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7408. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  7409. NSFileManager *fileManager = [NSFileManager defaultManager];
  7410. NSError *error = nil;
  7411. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  7412. if (error) { // 移动文件失败
  7413. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  7414. return [RAUtils dict2data:resultDictionary];
  7415. }
  7416. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  7417. pdf_path = [newPath lastPathComponent];
  7418. // 保存信息
  7419. NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (tearsheets_id,pdf_path,create_user,tear_note,tear_name,model_info,configureParams,is_local) values ((select ifnull(max(tearsheets_id),0) from offline_pdf) + 1,'%@','%@','%@','%@','%@','%@',1);",pdf_path,create_user,tear_note,tear_name,model_info,configureParams];
  7420. int result = [iSalesDB execSql:save_pdf_sql];
  7421. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7422. //
  7423. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  7424. if (remove_Item) {
  7425. // portfolioId
  7426. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  7427. }
  7428. return [RAUtils dict2data:resultDictionary];
  7429. }
  7430. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  7431. return [self offline_savePDF:params direct:YES];
  7432. }
  7433. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  7434. return [self offline_savePDF:params direct:NO];
  7435. }
  7436. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  7437. int offset = [[params valueForKey:@"offset"] integerValue];
  7438. int limit = [[params valueForKey:@"limit"] integerValue];
  7439. NSString *keyword = [params valueForKey:@"keyWord"];
  7440. NSString *where = @"where is_delete is null or is_delete = 0";
  7441. if (keyword.length) {
  7442. keyword = [self translateSingleQuote:keyword];
  7443. 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];
  7444. }
  7445. NSString *sql = [NSString stringWithFormat:@"select tear_name,tear_note,createtime,create_user,pdf_path,tearsheets_id from offline_pdf %@ order by createtime desc limit %d offset %d;",where,limit,offset];
  7446. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7447. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7448. NSString *cachefolder = [paths objectAtIndex:0];
  7449. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7450. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7451. NSString *name = [self textAtColumn:0 statement:stmt];
  7452. NSString *note = [self textAtColumn:1 statement:stmt];
  7453. NSString *time = [self textAtColumn:2 statement:stmt];
  7454. NSString *user = [self textAtColumn:3 statement:stmt];
  7455. NSString *path = [self textAtColumn:4 statement:stmt];
  7456. path = [pdfFolder stringByAppendingPathComponent:path];
  7457. time = [self changeDateTimeFormate:time];
  7458. time = [time stringByAppendingString:@" PST"];
  7459. int sheet_id = sqlite3_column_int(stmt, 5);
  7460. NSMutableDictionary *item = @{
  7461. @"tearsheetsId": @(sheet_id),
  7462. @"pdf_path": path,
  7463. @"create_time": time,
  7464. @"create_user": user,
  7465. @"tear_note": note,
  7466. @"tear_name": name,
  7467. @"isLocalFile":@"true"
  7468. }.mutableCopy;
  7469. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7470. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7471. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7472. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7473. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  7474. }];
  7475. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7476. return [RAUtils dict2data:dic];
  7477. }
  7478. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  7479. NSString *prodct_ids = [params objectForKey:@"product_id"];
  7480. // NSString *user = [params objectForKey:@"user"];
  7481. NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id,img) select * from (select p.product_id,p.name,p.description,p.item_id,i.url from product as p left join model_image as i on i.product_id = p.product_id where p.product_id in (%@) and p.product_id not in (select product_id from offline_portfolio) and i.type = 1 group by p.product_id order by i.default_img desc, i._id asc );insert into offline_portfolio (product_id,name,description,item_id,img,is_delete) select * from (select p.product_id,p.name,p.description,p.item_id,i.url,0 from product as p left join model_image as i on i.product_id = p.product_id where p.product_id in (%@) and p.product_id in (select product_id from offline_portfolio where is_delete = 1) and i.type = 1 and (select count(0) from offline_portfolio where product_id = p.product_id) = 1 group by p.product_id order by i.default_img desc, i._id asc );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];
  7482. int result = [iSalesDB execSql:sql];
  7483. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7484. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  7485. if (result == RESULT_TRUE) {
  7486. NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  7487. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];
  7488. }
  7489. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7490. return [RAUtils dict2data:dic];
  7491. }
  7492. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  7493. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  7494. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  7495. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  7496. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  7497. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  7498. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  7499. // {
  7500. // "count": 6,
  7501. // "val_1": {
  7502. // "value": "West Wholesale Price",
  7503. // "value_id": 17
  7504. // },
  7505. //
  7506. // }
  7507. __block long val_count = 0;
  7508. NSString *sql = @"select name,type,order_by from price order by order_by";
  7509. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7510. NSString *name = [self textAtColumn:0 statement:stmt];
  7511. int type = sqlite3_column_int(stmt, 1);
  7512. int order_by = sqlite3_column_int(stmt, 2);
  7513. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7514. NSDictionary *price_dic = @{
  7515. @"value" : name,
  7516. @"value_id" : [NSNumber numberWithInteger:type],
  7517. @"check" : order_by == 0 ? @(1) : @(0)
  7518. };
  7519. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  7520. val_count = ++(*count);
  7521. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7522. val_count = 0;
  7523. }];
  7524. [cadedate setObject:@{@"value" : @"None",
  7525. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  7526. val_count++;
  7527. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  7528. [price setObject:cadedate forKey:@"cadedate"];
  7529. [section1 setObject:price forKey:@"item_2"];
  7530. [dic setObject:section1 forKey:@"section_1"];
  7531. return [RAUtils dict2data:dic];
  7532. }
  7533. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  7534. NSString *product_id = [params objectForKey:@"fashionId"];
  7535. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  7536. __block int result = RESULT_TRUE;
  7537. __block int qty = 0;
  7538. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7539. qty = sqlite3_column_int(stmt, 0);
  7540. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7541. result = RESULT_FALSE;
  7542. }];
  7543. NSMutableDictionary *dic = @{
  7544. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  7545. @"mode" : @"Regular Mode",
  7546. @"quantity_available" : @(qty),
  7547. @"result" : @(result),
  7548. }.mutableCopy;
  7549. return [RAUtils dict2data:dic];
  7550. }
  7551. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  7552. NSString *item_ids = [params objectForKey:@"item_id"];
  7553. NSString *line_notes = [params objectForKey:@"notes"];
  7554. NSString *price_str = [params objectForKey:@"price"];
  7555. NSString *discount_str = [params objectForKey:@"discount"];
  7556. NSString *percent = [params objectForKey:@"available_percent"];
  7557. NSString *qty = [params objectForKey:@"available_qty"];
  7558. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  7559. int dot = 0;
  7560. if (line_notes) {
  7561. line_notes = [self translateSingleQuote:line_notes];
  7562. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  7563. sql = [sql stringByAppendingString:line_notes];
  7564. dot = 1;
  7565. } else {
  7566. line_notes = @"";
  7567. }
  7568. if (price_str) {
  7569. if (dot) {
  7570. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  7571. } else {
  7572. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  7573. dot = 1;
  7574. }
  7575. sql = [sql stringByAppendingString:price_str];
  7576. } else {
  7577. price_str = @"";
  7578. }
  7579. if (discount_str) {
  7580. if (dot) {
  7581. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  7582. } else {
  7583. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  7584. dot = 1;
  7585. }
  7586. sql = [sql stringByAppendingString:discount_str];
  7587. } else {
  7588. discount_str = @"";
  7589. }
  7590. if (percent) {
  7591. if (dot) {
  7592. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  7593. } else {
  7594. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  7595. dot = 1;
  7596. }
  7597. sql = [sql stringByAppendingString:percent];
  7598. } else {
  7599. percent = @"";
  7600. }
  7601. if (qty) {
  7602. if (dot) {
  7603. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  7604. } else {
  7605. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  7606. dot = 1;
  7607. }
  7608. sql = [sql stringByAppendingString:qty];
  7609. } else {
  7610. qty = @"";
  7611. }
  7612. NSString *where = [NSString stringWithFormat:@" where item_id in (%@);",item_ids];
  7613. sql = [sql stringByAppendingString:where];
  7614. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7615. int result = [iSalesDB execSql:sql];
  7616. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7617. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7618. return [RAUtils dict2data:dic];
  7619. }
  7620. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  7621. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  7622. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7623. sqlite3 *db = [iSalesDB get_db];
  7624. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set is_delete = 1 where item_id in (%@);",portfolio_ids];
  7625. int result = [iSalesDB execSql:sql db:db];
  7626. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7627. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7628. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  7629. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  7630. [iSalesDB close_db:db];
  7631. return [RAUtils dict2data:dic];
  7632. }
  7633. + (void)offline_removePDFWithName:(NSString *)name {
  7634. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7635. NSString *cachefolder = [paths objectAtIndex:0];
  7636. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7637. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  7638. NSFileManager *fileManager = [NSFileManager defaultManager];
  7639. [fileManager removeItemAtPath:path error:nil];
  7640. }
  7641. + (void)offline_clear_PDFCache {
  7642. NSFileManager *fileManager = [NSFileManager defaultManager];
  7643. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7644. NSString *cachefolder = [paths objectAtIndex:0];
  7645. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7646. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  7647. for (NSString *path in pdf_files) {
  7648. [self offline_removePDFWithName:[path lastPathComponent]];
  7649. }
  7650. }
  7651. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  7652. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7653. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  7654. NSString *user = [params objectForKey:@"user"];
  7655. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where tearsheets_id = %d;",tearsheetsId]];
  7656. if (![create_user isEqualToString:user]) {
  7657. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  7658. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7659. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  7660. return [RAUtils dict2data:dic];
  7661. }
  7662. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where tearsheets_id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  7663. __block int is_local = 0;
  7664. __block NSString *path = @"";
  7665. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7666. is_local = sqlite3_column_int(stmt, 0);
  7667. path = [self textAtColumn:1 statement:stmt];
  7668. }];
  7669. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where tearsheets_id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  7670. if (is_local == 1) {
  7671. sql = [NSString stringWithFormat:@"delete from offline_pdf where tearsheets_id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  7672. // 删除文件
  7673. [self offline_removePDFWithName:path];
  7674. }
  7675. int result = [iSalesDB execSql:sql];
  7676. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7677. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7678. return [RAUtils dict2data:dic];
  7679. }
  7680. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  7681. {
  7682. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7683. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  7684. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  7685. NSString* where=@"1=1";
  7686. if (ver!=nil) {
  7687. where=@"is_dirty=1";
  7688. }
  7689. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,name,description,item_id,fashion_id,available_qty,percentage,percent,sheet_price,sheet_discount,img,line_note,is_delete,createtime,modifytime from offline_portfolio where %@",where];
  7690. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  7691. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7692. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  7693. NSInteger _id = sqlite3_column_int(stmt, 0);
  7694. NSInteger product_id = sqlite3_column_int(stmt, 1);
  7695. NSString *name = [self textAtColumn:2 statement:stmt];
  7696. NSString *desc = [self textAtColumn:3 statement:stmt];
  7697. NSInteger item_id = sqlite3_column_int(stmt, 4);
  7698. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  7699. NSInteger qty = sqlite3_column_int(stmt, 6);
  7700. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  7701. double percent = sqlite3_column_double(stmt, 8);
  7702. double price = sqlite3_column_double(stmt, 9);
  7703. double discount = sqlite3_column_double(stmt, 10);
  7704. NSString *img = [self textAtColumn:11 statement:stmt];
  7705. NSString *line_note = [self textAtColumn:12 statement:stmt];
  7706. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  7707. NSString *create_time = [self textAtColumn:14 statement:stmt];
  7708. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  7709. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  7710. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  7711. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  7712. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  7713. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  7714. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  7715. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  7716. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  7717. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  7718. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  7719. [item setValue:name forKey:@"name"];
  7720. [item setValue:desc forKey:@"description"];
  7721. [item setValue:img forKey:@"img"];
  7722. [item setValue:line_note forKey:@"line_note"];
  7723. [item setValue:create_time forKey:@"createtime"];
  7724. [item setValue:modify_time forKey:@"modifytime"];
  7725. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7726. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7727. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  7728. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7729. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7730. }];
  7731. return ret;
  7732. }
  7733. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  7734. {
  7735. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7736. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  7737. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  7738. NSString* where=@"1=1";
  7739. if (ver!=nil) {
  7740. where=@"is_dirty=1";
  7741. }
  7742. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,tearsheets_id,pdf_path,create_user,tear_note,tear_name,model_info,createtime from offline_pdf where %@",where];
  7743. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  7744. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7745. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  7746. NSInteger _id = sqlite3_column_int(stmt, 0);
  7747. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  7748. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  7749. NSString *create_user = [self textAtColumn:3 statement:stmt];
  7750. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  7751. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  7752. NSString *model_info = [self textAtColumn:6 statement:stmt];
  7753. NSString *createtime = [self textAtColumn:7 statement:stmt];
  7754. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  7755. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  7756. [item setObject:pdf_path forKey:@"pdf_path"];
  7757. [item setObject:create_user forKey:@"create_user"];
  7758. [item setObject:tear_note forKey:@"tear_note"];
  7759. [item setObject:tear_name forKey:@"tear_name"];
  7760. [item setObject:model_info forKey:@"model_info"];
  7761. [item setObject:createtime forKey:@"createtime"];
  7762. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7763. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7764. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  7765. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7766. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7767. }];
  7768. return ret;
  7769. }
  7770. @end